javascript實(shí)現(xiàn)的左右無縫滾動效果
本文實(shí)例講述了javascript實(shí)現(xiàn)的左右無縫滾動效果。分享給大家供大家參考,具體如下:
前面介紹過圖片左右滾動,不過圖片是間歇性的一張一張滾動,今天介紹的是幾張圖片一起進(jìn)行無縫滾動,這是一個(gè)常用的 js 效果。
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>無縫滾動——左右</title>
<link rel="stylesheet" type="text/css" href="../css/base.css" media="all"/>
<style type="text/css">
#scroll{width:698px;height:108px;margin:50px auto 0;position:relative;overflow:hidden;}
.btn_left{display:block;width:68px;height:68px;background:url(images/btn.jpg) no-repeat -70px -69px;position:absolute;top:20px;left:1px;z-index:1;}
.btn_left:hover{background:url(images/btn.jpg) no-repeat -70px 0;}
.btn_right{display:block;width:68px;height:68px;background:url(images/btn.jpg) no-repeat 1px -69px;position:absolute;top:20px;right:0;z-index:1;}
.btn_right:hover{background:url(images/btn.jpg) no-repeat 1px 0;}
#scroll .content{width:546px;height:108px;position:relative;overflow:hidden;margin:0 auto;}
#scroll ul{position:absolute;}
#scroll li{float:left;width:182px;height:108px;text-align:center;}
#scroll li a:hover{position:relative;top:2px;}
</style>
</head>
<body>
<div id="scroll">
<a href="javascript:;" class="btn_left"></a>
<a href="javascript:;" class="btn_right"></a>
<div class="content">
<ul>
<li><a href="#"><img src="images/1.jpg" width="178" height="108" alt=""/></a></li>
<li><a href="#"><img src="images/2.jpg" width="178" height="108" alt=""/></a></li>
<li><a href="#"><img src="images/3.jpg" width="178" height="108" alt=""/></a></li>
<li><a href="#"><img src="images/4.jpg" width="178" height="108" alt=""/></a></li>
</ul>
</div>
</div>
</body>
</html>
<script type="text/javascript">
window.onload = function(){
var oDiv = document.getElementById('scroll');
var oUl = oDiv.getElementsByTagName('ul')[0];
var aLi = oDiv.getElementsByTagName('li');
var aBtn = oDiv.getElementsByTagName('a');
var speed = -1;
var timer = null;
oUl.innerHTML += oUl.innerHTML;
oUl.style.width = aLi[0].offsetWidth * aLi.length + 'px';
timer = setInterval(function(){
oUl.style.left = oUl.offsetLeft + speed + 'px';
if(oUl.offsetLeft < - oUl.offsetWidth / 2){
oUl.style.left = '0';
}else if(oUl.offsetLeft > 0){
oUl.style.left = - oUl.offsetWidth / 2 + 'px';
}
},30);
aBtn[0].onclick = function(){
speed = -1;
};
aBtn[1].onclick = function(){
speed = 1;
};
oUl.onmouseover = function(){
clearInterval(timer);
};
oUl.onmouseout = function(){
timer = setInterval(function(){
oUl.style.left = oUl.offsetLeft + speed + 'px';
if(oUl.offsetLeft < -oUl.offsetWidth / 2){
oUl.style.left = '0';
}else if(oUl.offsetLeft > 0){
oUl.style.left = - oUl.offsetWidth / 2 + 'px';
}
},30);
};
};
</script>
PS:如果想要改變移動速度,只需要改變 speed 的值。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。
- js實(shí)現(xiàn)圖片無縫滾動特效
- 徹底搞懂JS無縫滾動代碼
- div+css+js實(shí)現(xiàn)無縫滾動類似marquee無縫滾動兼容firefox
- js 實(shí)現(xiàn)無縫滾動 兼容IE和FF
- js向上無縫滾動,網(wǎng)站公告效果 具體代碼
- jcarousellite.js 基于Jquery的圖片無縫滾動插件
- js實(shí)現(xiàn)可控制左右方向的無縫滾動效果
- js實(shí)現(xiàn)文字列表無縫滾動效果
- JQuery插件Marquee.js實(shí)現(xiàn)無縫滾動效果
- JavaScript定時(shí)器實(shí)現(xiàn)無縫滾動圖片
相關(guān)文章
使用JavaScript實(shí)現(xiàn)鏈表的數(shù)據(jù)結(jié)構(gòu)的代碼
鏈表(Linked list)是一種常見的基礎(chǔ)數(shù)據(jù)結(jié)構(gòu),是一種線性表,但是并不會按線性的順序存儲數(shù)據(jù),而是在每一個(gè)節(jié)點(diǎn)里存到下一個(gè)節(jié)點(diǎn)的指針(Pointer) 。下面我們用 JavaScript 代碼對鏈表的數(shù)據(jù)結(jié)構(gòu)進(jìn)行實(shí)現(xiàn)2017-08-08
js實(shí)現(xiàn)按鈕控制帶有停頓效果的圖片滾動
這篇文章主要介紹了js實(shí)現(xiàn)按鈕控制帶有停頓效果的圖片滾動,,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
關(guān)于JAVASCRIPT urldecode URL解碼的問題
JS要實(shí)現(xiàn)單純的編碼解碼輕而易舉,escape、unescape就搞定,但是遇到不是完整的URL轉(zhuǎn)碼,就沒辦法了2012-01-01
小程序hover-class點(diǎn)擊態(tài)效果實(shí)現(xiàn)
這篇文章主要介紹了小程序hover-class點(diǎn)擊態(tài)效果實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
原生javascript自定義input[type=radio]效果示例
這篇文章主要介紹了原生javascript自定義input[type=radio]效果,結(jié)合實(shí)例形式分析了javascript模擬form表單中radio效果的相關(guān)操作技巧,需要的朋友可以參考下2019-08-08

