js實(shí)現(xiàn)圖片無(wú)縫滾動(dòng)
js無(wú)縫滾動(dòng)效果幾乎在任何網(wǎng)頁(yè)上都能看到它的身影,有的可能是使用插件,其實(shí)使用原始的javascript比較簡(jiǎn)單。
主要的是使用js位置知識(shí)。
- 1.innerHTML:設(shè)置或獲取元素的html標(biāo)簽
- 2.scrollLeft:設(shè)置或獲取位于對(duì)象左邊界和窗口中目前可見(jiàn)內(nèi)容的最左端之間的距
- 3.offsetWidth:設(shè)置或獲取指定標(biāo)簽的寬度
- 4.setInterval():設(shè)置方法定時(shí)啟動(dòng)
- 5.clearInterval();清除定時(shí)器
效果圖:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>javascript scroll制作</title>
</head>
<body>
<style>
/*conment*/
*{
margin: 0;
padding: 0;
}
img{max-width: 100%;}
.container{
max-width: 620px;
margin: 0 auto;
padding-top: 50px;
}
.text-center{text-align: center;}
.list-inline li{
display: inline-block;
}
.hide{display: none;}
hr{
margin:20px 0;
}
.tag{
background-color: #ccc;
padding: 5px 0;
}
.tag li{
padding: 0 10px;
border-left: 1px solid #fff;
cursor:pointer;
}
.tag li:first-child{
border-left: transparent;
}
.tag li.active{
background-color: #ddd;
}
.scroll{
position: relative;
padding: 10px;
margin-bottom: 20px;
background-color: #ddd;
}
.wrap{
overflow: hidden;
}
.content{
min-width: 3000px;
height: 200px;
}
.content ul{
float: left;
}
.content ul li{
display: inline-block;
max-width: 200px;
}
#prev,#next{
width: 50px;
height: 50px;
margin-top: -25px;
background-color: #ccc;
line-height: 50px;
text-align: center;
cursor: pointer;
}
#prev{
position: absolute;
left: 0;
top:50%;
border-radius: 0 25px 25px 0;
}
#next{
position: absolute;
right: 0;
top:50%;
border-radius: 25px 0 0 25px;
}
</style>
<div class="container">
<h1 class="text-center">圖片滾動(dòng)制作</h1>
<hr>
<div class="scroll">
<div class="wrap" id="wrap">
<div id="content" class="content" >
<ul id="list1">
<li> <img src="freelance.gif" alt=""> </li>
<li> <img src="button.gif" alt=""></li>
<li> <img src="load.gif" alt=""></li>
<li> <img src="straw.gif" alt=""></li>
</ul>
<ul id="list2">
</ul>
</div>
</div>
<div id="prev">
prev
</div>
<div id="next">
next
</div>
</div>
</div>
<script>
var wrap=document.getElementById('wrap');
var list1=document.getElementById('list1');
var list2=document.getElementById('list2');
var prev=document.getElementById('prev');
var next=document.getElementById('next');
//創(chuàng)建復(fù)制一份內(nèi)容列表
list2.innerHTML=list1.innerHTML;
//向左循環(huán)滾動(dòng)
function scroll(){
if(wrap.scrollLeft>=list2.offsetWidth){
wrap.scrollLeft=0;
}
else{
wrap.scrollLeft++;
}
}
timer = setInterval(scroll,1);
//鼠標(biāo)停留使用clearInterval()
wrap.onmouseover=function(){
clearInterval(timer);
}
wrap.onmouseout=function(){
timer = setInterval(scroll,1);
}
//向左加速
function scroll_l(){
if(wrap.scrollLeft>=list2.offsetWidth){
wrap.scrollLeft=0;
}
else{
wrap.scrollLeft++;
}
}
//向右滾動(dòng)
function scroll_r(){
if(wrap.scrollLeft<=0){
wrap.scrollLeft+=list2.offsetWidth;
}
else{
wrap.scrollLeft--;
}
}
prev.onclick=function(){
clearInterval(timer);
change(0)
}
next.onclick=function(){
clearInterval(timer);
change(1)
}
function change(r){
if(r==0){
timer = setInterval(scroll_l,60);
wrap.onmouseout = function(){
timer = setInterval(scroll_l,60);
}
}
if(r==1){
timer = setInterval(scroll_r,60);
wrap.onmouseout = function(){
timer = setInterval(scroll_r,60);
}
}
}
</script>
</body>
</html>
以上就是為大家分享的js實(shí)現(xiàn)圖片無(wú)縫滾動(dòng)代碼,希望對(duì)大家的學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
- javascript實(shí)現(xiàn)多張圖片左右無(wú)縫滾動(dòng)效果
- js實(shí)現(xiàn)圖片無(wú)縫滾動(dòng)特效
- Javascript 實(shí)現(xiàn)圖片無(wú)縫滾動(dòng)
- 使用Javascript簡(jiǎn)單實(shí)現(xiàn)圖片無(wú)縫滾動(dòng)
- 可自定義速度的js圖片無(wú)縫滾動(dòng)示例分享
- JS圖片無(wú)縫滾動(dòng)(簡(jiǎn)單利于使用)
- jcarousellite.js 基于Jquery的圖片無(wú)縫滾動(dòng)插件
- JS實(shí)現(xiàn)單張或多張圖片持續(xù)無(wú)縫滾動(dòng)的示例代碼
相關(guān)文章
記錄一篇關(guān)于redux-saga的基本使用過(guò)程
這篇文章主要介紹了記錄一篇redux-saga的基本使用過(guò)程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
javaScript實(shí)現(xiàn)滾動(dòng)新聞的方法
這篇文章主要介紹了javaScript實(shí)現(xiàn)滾動(dòng)新聞的方法,涉及javascript實(shí)現(xiàn)頁(yè)面滾動(dòng)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
一步快速解決微信小程序中textarea層級(jí)太高遮擋其他組件
這篇文章主要給大家介紹了關(guān)于如何通過(guò)一步快速解決微信小程序中textarea層級(jí)太高遮擋其他組件問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
JavaScript實(shí)現(xiàn)添加及刪除事件的方法小結(jié)
這篇文章主要介紹了JavaScript實(shí)現(xiàn)添加及刪除事件的方法,實(shí)例總結(jié)了javascript對(duì)事件的添加及刪除的技巧,涉及javascript事件綁定的方法及瀏覽器兼容的相關(guān)注意事項(xiàng),需要的朋友可以參考下2015-08-08
解決WordPress使用CDN后博文無(wú)法評(píng)論的錯(cuò)誤
這篇文章主要介紹了解決WordPress使用CDN后博文無(wú)法評(píng)論的錯(cuò)誤的方法,同時(shí)提醒注意WordPress使用版本的jQuery版本支持度,需要的朋友可以參考下2015-12-12
解決bootstrap導(dǎo)航欄navbar在IE8上存在缺陷的方法
這篇文章主要為大家詳細(xì)介紹了解決bootstrap導(dǎo)航欄navbar在IE8上存在缺陷的方法,需要的朋友可以參考下2016-07-07
使用JS來(lái)動(dòng)態(tài)操作css的幾種方法
這篇文章主要介紹了使用JS來(lái)動(dòng)態(tài)操作css的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
找出字符串中出現(xiàn)次數(shù)最多的字母和出現(xiàn)次數(shù)精簡(jiǎn)版
找出字符串中出現(xiàn)次數(shù)最多的字母和出現(xiàn)次數(shù)精簡(jiǎn)版,有需求的朋友可以參考下2012-11-11

