WEB前端:05_scroll滚动(图片/文字滚动)

scroll滚动(图片/文字滚动)

 

网站常用效果之一,以下为简化版,用于学习javascript基础知识。

 

效果图:

 

 

scroll滚动(图片/文字滚动)- 纯JS简化版

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>scroll滚动 - 纯js简化版</title>
<style type="text/css">
*{margin: 0; padding: 0;}
#probox{ position: relative; border: 1px solid #ccc; width:1000px;overflow:hidden;height:120px; margin: 10px auto;}
#prolist{ position: relative; width:2000px; height: 120px; list-style: none; margin: 0; padding: 0;}
#prolist li {float: left; width: 250px; height: 120px; text-align: center;}
#left,#right{ background: #333; color: #fff; padding: 5px; cursor:pointer;}
</style>
<script type="text/javascript">
window.onload = function() {
    var probox = document.getElementById(‘probox‘);
    var prolist = document.getElementById(‘prolist‘);
    var prolistli = prolist.getElementsByTagName(‘li‘);
    var btnleft = document.getElementById(‘left‘);
    var btnright = document.getElementById(‘right‘);
 
    var cut = prolistli[1].offsetLeft - prolistli[0].offsetLeft;
    var len = prolist.offsetLeft;
    var timer = null;
 
    function autoplay() {
        if(timer) {
            clearInterval(timer);
        }
        timer = setInterval(function() {
            if(prolist.offsetLeft == -cut) {
                clearInterval(timer);
                prolist.appendChild(prolistli[0]);
                prolist.style.left = "0px";
            } else {
                prolist.style.left = prolist.offsetLeft - 10 + "px";
            }
        }, 30)
    }
 
    probox.onmouseover = function() {
        clearInterval(o);
    }
 
    probox.onmouseout = function() {
        o = setInterval(autoplay, 3000);
    }
 
    btnleft.onclick = function() {
        clearInterval(o);
        if(timer) {
            clearInterval(timer);
        }
         
        timer = setInterval(function() {
            if(prolist.offsetLeft == 0) {
                clearInterval(timer);
                prolist.insertBefore(prolistli[prolistli.length - 1], prolist.firstChild);
                prolist.style.left = -cut + "px";
            } else {
                prolist.style.left = prolist.offsetLeft + 10 + "px";
            }
        }, 30);
 
        o = setInterval(autoplay, 3000);
    }
 
    btnright.onclick = function() {
        clearInterval(o);
        if(timer) {
            clearInterval(timer);
        }
        timer = setInterval(function() {
            if(prolist.offsetLeft == -cut) {
                clearInterval(timer);
                prolist.appendChild(prolistli[0]);
                prolist.style.left = "0px";
            } else {
                prolist.style.left = prolist.offsetLeft - 10 + "px";
            }
        }, 30);
 
        o = setInterval(autoplay, 3000);
    }
 
    o = setInterval(autoplay, 3000);
 
}
</script>
</head>
<body>
 
<div id="probox">
    <ul id="prolist">
        <li><img src="tab1.jpg" width="220" height="120" /></li>
        <li><img src="tab2.jpg" width="220" height="120" /></li>
        <li><img src="tab3.jpg" width="220" height="120" /></li>
        <li><img src="tab4.jpg" width="220" height="120" /></li>
        <li><img src="tab5.jpg" width="220" height="120" /></li>
        <li><img src="tab6.jpg" width="220" height="120" /></li>
        <li><img src="tab7.jpg" width="220" height="120" /></li>
        <li><img src="tab8.jpg" width="220" height="120" /></li>
    </ul>
</div>
<span id="left">上一个</span> <span id="right">下一个</span>
 
</body>
</html>

 

WEB前端:05_scroll滚动(图片/文字滚动),古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。