图片上下滑动(鼠标点击才动)

实现代码:

HTML+CSS:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<style>
    *{margin: 0px;padding: 0px;}
    body{background-color: #eee;padding: 100px;}
    a{text-decoration: none;}
    b{display: block;height: 40px;width: 40px;line-height: 40px;border-radius: 40px;background-color: #117b1c;color: #fff;text-align: center;cursor: pointer;}
    #box{position: relative;height: 312px;width: 92px;padding: 10px;border: 1px solid #ccc;overflow: hidden;}
    #box ul{position: absolute;top: 0;left: 0;}
    #box ul li{list-style: none;height: 100px;width: 100px;padding: 5px;}
    #box ul li a{display: block;width: 100px;height: 100px;border: 1px solid #000;}
    #box ul li.no1 a{background-color: #99CC33;}
    #box ul li.no2 a{background-color: #FF6666;}
    #box ul li.no3 a{background-color: #336699;}
    #box ul li.no4 a{background-color: #99CCCC;}
</style>
</head>
<body>
    <b></b>
    <div id="box">
        <ul>
            <li class="no1"><a href="javascript:;'">111</a></li>
            <li class="no2"><a href="javascript:;'">222</a></li>
            <li class="no3"><a href="javascript:;'">333</a></li>
            <li class="no4"><a href="javascript:;'">444</a></li>
        </ul>
    </div>
    <b></b>
</body>
</html>

js:

<script>
    window.onload=function(){
        var oBox = document.getElementById('box');
        var oUl = oBox.getElementsByTagName('ul')[0];
        var aLi = oUl.getElementsByTagName('li');
        var aBtn = document.getElementsByTagName('b');
        var timer = null;
        var iSpeed = -5;

        oUl.innerHTML+=oUl.innerHTML;

        aBtn[0].οnmοusedοwn=function(){
            iSpeed = -5;
            timer = setInterval(doMove,30);
        };

        aBtn[0].οnmοuseup=function(){
            clearInterval(timer);
        };

        aBtn[1].οnmοusedοwn=function(){
            iSpeed = 5;
            timer = setInterval(doMove,30);
        };

        aBtn[1].οnmοuseup=function(){
            clearInterval(timer);
        };

        function doMove(){
            oUl.style.top = oUl.offsetTop + iSpeed +'px';

            if(oUl.offsetTop < -oUl.offsetHeight/2){
                oUl.style.top = '0';
            }
            else if(oUl.offsetTop > 0){
                oUl.style.top = -oUl.offsetHeight/2 + 'px';
            }
        };

    }
</script>

效果如下图所示:

 

转载于:https://www.cnblogs.com/baixc/p/3435178.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值