js實(shí)現(xiàn)無(wú)縫滾動(dòng)圖(可控制當(dāng)前滾動(dòng)的方向)
這個(gè)版本可以控制左右滾動(dòng),鼠標(biāo)點(diǎn)擊對(duì)應(yīng)的廣告會(huì)自動(dòng)滑動(dòng)把廣告完全展示出來(lái)。還實(shí)現(xiàn)了記錄當(dāng)前滾動(dòng)的方向,當(dāng)鼠標(biāo)離開,接著繼續(xù)滾動(dòng)?。?!
效果圖:

代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style type = "text/css">
*{margin: 0; padding: 0;}
li { list-style: none; }
.box {
width: 800px;
height: 450px;
margin: 50px auto;
overflow: hidden;
position: relative;
}
.box span {
width: 40px;
height: 60px;
display: block;
position: absolute;
top: 225px;
margin-top: -20px;
cursor: pointer;
z-index: 1;
}
.box #left {
background: url('http://cdn.attach.qdfuns.com/notes/pics/201701/14/042311cbd7gbjd7sggkd2b.png') no-repeat;
left: 0;
display: none;
}
.box #right {
background: url('http://cdn.attach.qdfuns.com/notes/pics/201701/14/042241w8z4hx4m4pjhyjzs.png') no-repeat;
right: 0;
display: none;
}
#ad {
width: 4000px;
height: 450px;
position: absolute;
}
#ad li {
float: left;
}
</style>
<script type = "text/javascript">
window.onload = function(){
var ad = document.getElementById("ad");
var lef = document.getElementById("left");
var rig = document.getElementById("right");
var timer = null; //管理定時(shí)器
var aspect = true;
function animate(obj,speed){
//關(guān)閉上一個(gè)定時(shí)器
clearInterval(obj.timer);
//管理定時(shí)器
obj.timer = setInterval(autoAd,30);
function autoAd(){
//判斷左走或者右走
if(speed > 0){
aspect = true;
if(obj.offsetLeft >= 0){
obj.style.left = -3200 + 'px';
}
}else {
aspect = false;
if(obj.offsetLeft <= -3200){
obj.style.left = 0;
}
}
//勻速動(dòng)畫: 盒子當(dāng)前的位置 + 步長(zhǎng)
obj.style.left = obj.offsetLeft + speed +"px";
}
}
animate(ad,-5);
//鼠標(biāo)劃入顯示控制按鈕并關(guān)閉
ad.parentNode.onmouseover = function(){
clearInterval(ad.timer);
lef.style.display = "block";
rig.style.display = "block";
};
//鼠標(biāo)離開隱藏控制按鈕并啟動(dòng)定時(shí)器
ad.parentNode.onmouseout = function(){
clearInterval(ad.timer);
clearInterval(timer);
lef.style.display = "none";
rig.style.display = "none";
if(aspect){
animate(ad,5);
}else{
animate(ad,-5);
}
};
ad.onclick = function(event){
//關(guān)閉自動(dòng)輪播定時(shí)器
clearInterval(ad.timer);
clearInterval(timer);
var event = event || window.event;
if(event.target){
var target = - parseInt(event.target.alt) * 800;
}else{
var target = - parseInt(event.srcElement.alt) * 800;
}
timer = setInterval(function(){
var step = (target - ad.offsetLeft) / 10;
step = step > 0 ? Math.ceil(step):Math.floor(step);
ad.style.left = ad.offsetLeft + step + "px";
if(ad.offsetLeft%800 == 0){
clearInterval(timer);
}
},20)
}
//右移動(dòng)
lef.onclick = function(){
clearInterval(timer);
animate(ad,5);
};
//左移動(dòng)
rig.onclick = function(){
clearInterval(timer);
animate(ad,-5);
};
}
</script>
</head>
<body>
<div class="box">
<ul id="ad">
<li><img src="http://cdn.attach.qdfuns.com/notes/pics/201701/14/042153lscsitwp7sszb6zs.jpg" alt="0"></li>
<li><img src="http://cdn.attach.qdfuns.com/notes/pics/201701/14/042152pfbkbfe8vbtvulfu.jpg" alt="1"></li>
<li><img src="http://cdn.attach.qdfuns.com/notes/pics/201701/14/042153u3jgn0ds43ndd3dz.jpg" alt="2"></li>
<li><img src="http://cdn.attach.qdfuns.com/notes/pics/201701/14/042152juuohne22z60hbsb.jpg" alt="3"></li>
<li><img src="http://cdn.attach.qdfuns.com/notes/pics/201701/14/042153lscsitwp7sszb6zs.jpg" alt="4"></li>
</ul>
<span id="left"></span>
<span id="right"></span>
</div>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- 徹底搞懂JS無(wú)縫滾動(dòng)代碼
- js 實(shí)現(xiàn)無(wú)縫滾動(dòng) 兼容IE和FF
- div+css+js實(shí)現(xiàn)無(wú)縫滾動(dòng)類似marquee無(wú)縫滾動(dòng)兼容firefox
- jcarousellite.js 基于Jquery的圖片無(wú)縫滾動(dòng)插件
- js向上無(wú)縫滾動(dòng),網(wǎng)站公告效果 具體代碼
- js實(shí)現(xiàn)圖片無(wú)縫滾動(dòng)特效
- 走馬燈效果代碼js appendChild實(shí)現(xiàn)的無(wú)縫滾動(dòng)
- JS左右無(wú)縫滾動(dòng)(一般方法+面向?qū)ο蠓椒ǎ?/a>
- Jquery與JS兩種方法仿twitter/新浪微博 高度自適應(yīng)無(wú)縫滾動(dòng)實(shí)現(xiàn)代碼
- js實(shí)現(xiàn)無(wú)縫滾動(dòng)圖
相關(guān)文章
javascript實(shí)現(xiàn)的仿51job地址多項(xiàng)選擇方式效果
分享一個(gè)類似51job方式的地址選擇效果2009-12-12
通過(guò)MSXML2自動(dòng)獲取QQ個(gè)人頭像及在線情況(給初學(xué)者)
通過(guò)MSXML2自動(dòng)獲取QQ個(gè)人頭像及在線情況(給初學(xué)者)...2007-01-01
layui內(nèi)置模塊layim發(fā)送圖片添加加載動(dòng)畫的方法
今天小編就為大家分享一篇layui內(nèi)置模塊layim發(fā)送圖片添加加載動(dòng)畫的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
JS輸出空格的簡(jiǎn)單實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇JS輸出空格的簡(jiǎn)單實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09

