javascript實(shí)現(xiàn)的左右無縫滾動(dòng)效果
本文實(shí)例講述了javascript實(shí)現(xiàn)的左右無縫滾動(dòng)效果。分享給大家供大家參考,具體如下:
前面介紹過圖片左右滾動(dòng),不過圖片是間歇性的一張一張滾動(dòng),今天介紹的是幾張圖片一起進(jìn)行無縫滾動(dòng),這是一個(gè)常用的 js 效果。
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>無縫滾動(dòng)——左右</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:如果想要改變移動(dòng)速度,只需要改變 speed 的值。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- js實(shí)現(xiàn)圖片無縫滾動(dòng)特效
- 徹底搞懂JS無縫滾動(dòng)代碼
- div+css+js實(shí)現(xiàn)無縫滾動(dòng)類似marquee無縫滾動(dòng)兼容firefox
- js 實(shí)現(xiàn)無縫滾動(dòng) 兼容IE和FF
- js向上無縫滾動(dòng),網(wǎng)站公告效果 具體代碼
- jcarousellite.js 基于Jquery的圖片無縫滾動(dòng)插件
- js實(shí)現(xiàn)可控制左右方向的無縫滾動(dòng)效果
- js實(shí)現(xiàn)文字列表無縫滾動(dòng)效果
- JQuery插件Marquee.js實(shí)現(xiàn)無縫滾動(dòng)效果
- JavaScript定時(shí)器實(shí)現(xiàn)無縫滾動(dòng)圖片
相關(guān)文章
Cropper.js進(jìn)階之裁剪后保存至本地實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Cropper.js進(jìn)階之裁剪后保存至本地實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05javascript 用記憶函數(shù)快速計(jì)算遞歸函數(shù)
摘自《JavaScript: The Good Parts》,作為讀書筆記備用。對(duì)于追求執(zhí)行效率的朋友可以參考下。2010-03-03JS獲取浮動(dòng)(float)元素的style.left值為空的快速解決辦法
這篇文章主要介紹了JS獲取浮動(dòng)(float)元素的style.left值為空的快速解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02Display SQL Server Version Information
Display SQL Server Version Information...2007-06-06從URL中提取參數(shù)與將對(duì)象轉(zhuǎn)換為URL查詢參數(shù)的實(shí)現(xiàn)代碼
這兩種主要是對(duì)《Prototype淺析》先前略過的Sring部分中toQueryParams和Object部分的toQueryString方法的補(bǔ)充2012-01-01ES6 Promise對(duì)象的應(yīng)用實(shí)例分析
這篇文章主要介紹了ES6 Promise對(duì)象的應(yīng)用,結(jié)合實(shí)例形式分析了Promise對(duì)象原理與異步處理相關(guān)操作技巧,需要的朋友可以參考下2019-06-06swiper 解決動(dòng)態(tài)加載數(shù)據(jù)滑動(dòng)失效的問題
下面小編就為大家分享一篇swiper 解決動(dòng)態(tài)加載數(shù)據(jù)滑動(dòng)失效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02