zoukankan      html  css  js  c++  java
  • 无缝滚动

    第一种方法:

    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style>
            *{
                margin:0px;
                padding:0px;
            }
                #box{
                    position: relative;
                    880px;
                    height:220px;
                    margin:50px auto;
                    border:1px solid #999;
                    overflow: hidden;
                }
                #box ul{   /*ul不能设置宽度*/
                    height:200px;
                    position:absolute;
                    top:0;
                    left:0px;
                    padding:10px 0;
                }
                #box ul li{
                    200px;
                    height:200px;
                    float:left;
                    padding:0 10px;
                    list-style: none;
                }
                #box ul li img{
                    200px;
                    height:200px;
                }
            </style>
            <script>
                window.onload=function(){
                    var oBox=document.getElementById("box");
                    var oUl=oBox.children[0];
                    var aLi=oUl.getElementsByTagName('li');
                    var oLeft=document.getElementById("left");
                    var oRight=document.getElementById("right");
                    var timer=null;
                    var speed=0;
                    oUl.innerHTML+=oUl.innerHTML;          //复制一份oUl的内容
                    oUl.style.width=aLi[0].offsetWidth*aLi.length+'px';//设置oUl的宽度
                    clearInterval(timer);
                    function tab(){
                        var l=oUl.offsetLeft+speed;
                                                    //向左运动,当l小于oUl宽度的一半时,l=0
                        if(l<=-oUl.offsetWidth/2){   
                            l=0;
                        }
                                                    //向右运动,当l大于0时,l=oUl宽度的一半
                        if(l>0){l=-oUl.offsetWidth/2}  
                        oUl.style.left=l+'px';
                    }
                    timer=setInterval(tab,30);
                    oLeft.onclick=function(){
                        speed=-6;
                    }
                    oRight.onclick=function(){
                        speed=6;
                    }
                    oBox.onmouseover=function(){
                        clearInterval(timer);
                    }
                    oBox.onmouseout=function(){
                        timer=setInterval(tab,30);
                    }
                    document.onkeydown=function(ev){
                        var e=ev||event;
                        if(e.keyCode==37){
                            speed=-6;
                        }
                        if(e.keyCode==39){
                            speed=6;
                        }
                    }
                }
            </script>
        </head>
        <body>
            <input type="button" id="left" value="向左"/>
            <input type="button" id="right" value="向右"/>
            <div id="box">
                <ul>
                    <li><img src="img/0.jpg"></img></li>
                    <li><img src="img/1.jpg"></img></li>
                    <li><img src="img/2.jpg"></img></li>
                    <li><img src="img/3.jpg"></img></li>
                </ul>
            </div>
        </body>
    </html>

    第二种方法:

    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style>
            *{
                margin:0px;
                padding:0px;
            }
                #box{
                    position: relative;
                    880px;
                    height:220px;
                    margin:50px auto;
                    border:1px solid #999;
                    overflow: hidden;
                }
                #box ul{
                    height:200px;
                    position:absolute;
                    top:0;
                    left:0px;
                    padding:10px 0;
                }
                #box ul li{
                    200px;
                    height:200px;
                    float:left;
                    padding:0 10px;
                    list-style: none;
                }
                #box ul li img{
                    200px;
                    height:200px;
                }
            </style>
            <script>
                window.onload=function(){
                    var oBox=document.getElementById("box");
                    var oUl=oBox.children[0];
                    var aLi=oUl.getElementsByTagName('li');
                    var oLeft=document.getElementById('left');
                    var oRight=document.getElementById('right');
                    var timer=0;
                    oUl.innerHTML+=oUl.innerHTML;
                    oUl.style.width=aLi[0].offsetWidth*aLi.length+'px';
                    var w=oUl.offsetWidth/2;  
                    var l=0;                    //走的距离
                    var speed=0;       //运动的速度
                    function tab(){
                        l+=speed;
                        oUl.style.left=(l%w-w)%w+'px';  //l%w保证向右的left不会超过0,保证向左的l等于left
                    }
                    clearInterval(timer);
                    timer=setInterval(tab,30);
                    oLeft.onclick=function(){
                        speed=-6;
                    }
                    oRight.onclick=function(){
                        speed=6;
                    }
                    oBox.onmouseover=function(){
                        clearInterval(timer);
                    }
                    oBox.onmouseout=function(){
                        timer=setInterval(tab,30);
                    }
                    document.onkeydown=function(ev){
                        var e=ev||event;
                        if(e.keyCode==37){speed=-6}
                        if(e.keyCode==39){speed=6}
                    }
                }
            </script>
        </head>
        <body>
            <input type="button" id="left" value="向左" />
            <input type="button" id="right" value="向右" />
            <div id="box">
                
                <ul>
                    <li><img src="img/0.jpg"></img></li>
                    <li><img src="img/1.jpg"></img></li>
                    <li><img src="img/2.jpg"></img></li>
                    <li><img src="img/3.jpg"></img></li>
                </ul>
            </div>
        </body>
    </html>
  • 相关阅读:
    输人10个学生5门课的成绩,分别用函数实现下列功能:①计算每个学生的平均分; ②计算每门课的平均分; ③找出所有50个分数中最高的分数所对应的学生和课程; ④计算平均分方差: 其中,x;为某一学生的平均分。
    用递归方法求n阶勒让德多项式的值,递归公式为
    用牛顿迭代法求根。方程为ax^3+bx^2 +cx+d=0,系数a,b,c,d的值依次为1,2,3,4,由主函数输人。求x在1附近的一个实根。求出根后由主函数输出
    写一个函数,用起泡法对输人的10个字符按由小到大顺序排列
    写一个函数,输人一行字符,将此字符串中最长的单词输出
    HEX文件格式
    Linux .bashrc
    Linux dd烧写系统
    以太坊代币与账户交易
    记录智能合约solidity编译的坑
  • 原文地址:https://www.cnblogs.com/yang0902/p/5705300.html
Copyright © 2011-2022 走看看