zoukankan      html  css  js  c++  java
  • 定时器简单案例-1

    长图左右滚动:

     示例代码:

    1.HTML代码:

    <!DOCTYPE hml>
    <html>
    <head>
       <meta charset="utf-8">
       <title>长图滚动</title>
     <link el="stylesheet" href="./style.css">
    </head>
    <body>
       <div id="box">
                <img src="./images/long_pic2.jpg" id="pic">
                <span id="to-left"></span>
                <span id="to_right"></span>
        </div>
        <script src="./index.js"></script>
    </body>
    </html>

    2.css:样式表文件

                *{
                    padding: 0;
                    margin: 0;
                    list-style: none;
                }
                body{
                    background: url("./images/move.gif") no-repeat;
                    background-size: cover;
                }
                #box{
                    width: 800px;
                    height: 400px;
                    margin: 100px auto;
                    border: 2px solid pink;
                    box-shadow: 0 0 10px red;
                    overflow: hidden;
                    position: relative;
    
                }
                #pic{
                    height: 400px;
                    position: absolute;
                    left: 0;
                    top: 0;
                }
                #to-left,#to_right{
                    width: 50%;
                    height: 100%;
                    cursor: pointer;
                    position: absolute;
                    top: 0;
                }
                #to-left{
                    left: 0;
                    /*background-color: red;*/
                }
                #to_right{
                    right: 0;
                    /*background-color: green;*/
                }

    3.js文件:

                window.onload=function(){
                    var timer=null;
                    var num=0;
                   //监听鼠标移入事件
                   //1.图片向左移动
                   $("to-left").onmouseover=function(){
                       //清除定时器
                       clearInterval(timer);
                       //开启定时器
                       timer=setInterval(function(){
                           //设置移动步长
                           num-=5;
                           //判断,图片可视宽度=图片真实宽度-box宽度
                           if(num>=-1710){
                               $("pic").style.left=num+"px";
                           }else{
                               clearInterval(timer);
                           }
                       },30);
                   }
                   //2.图片右移
                   $("to_right").onmouseover=function(){
                       //清除定时器
                       clearInterval(timer);
                       //开启定时器
                       timer=setInterval(function(){
                           //设置步长
                           num+=5;
                           //判断
                           if(num<5){
                               $("pic").style.left=num+"px";
                           }else{
                               clearInterval(timer);
                           }
                       },30);
                   }
                   //监听盒子鼠标移出事件
                   $("box").onmouseout=function(){
                       //清除定时器
                       clearInterval(timer);
                   }
                }
                function $(id){
                    return typeof id==="string"?document.getElementById(id):null;
                }

    3.效果展示:

  • 相关阅读:
    @Autowired和@Resource的区别是什么?
    关于事务,事务的特性,spring事务的传播特性
    Java 用Freemarker完美导出word文档(带图片)
    关于 MySQL查询当天、本周,本月,上一个月的数据
    js如何使用radio
    Freemarker提供了3种加载模板目录的方法
    190707Python-MySQL
    190707Python-RabbitMQ
    190707select和selector模块
    4、kubernetes资源清单快速入门190625
  • 原文地址:https://www.cnblogs.com/zhang-jiao/p/9693321.html
Copyright © 2011-2022 走看看