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.效果展示:

  • 相关阅读:
    购物网站数据库表
    C#Excel的导入与导出
    DataTable过滤重复字段
    压力测试~一套完整的压力测试项目文档
    压力测试~测试工具的使用
    C#调用本机摄像头
    linq学习笔记
    EasyPlayer RTSP播放器对RTSP播放地址url的通用兼容修改意见
    我们计划为EasyDSS定制开发一款超低延时的EasyPlayer Flash播放器
    EasyRTMP+EasyDSS实现一套完整的紧急视频回传直播与存储回放方案
  • 原文地址:https://www.cnblogs.com/zhang-jiao/p/9693321.html
Copyright © 2011-2022 走看看