javascript實(shí)現(xiàn)列表滾動(dòng)的方法
本文實(shí)例講述了javascript實(shí)現(xiàn)列表滾動(dòng)的方法。分享給大家供大家參考。具體如下:
index.html如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<link rel="stylesheet" type="text/css" href="css/global.css" />
<script type="text/javascript" src="js/jquery-1.7.min.js"></script>
<script type="text/javascript" src="js/scroll_img.js"></script>
<title>圖片列表滾動(dòng)</title>
</head>
<body>
<div class="scroll_img_wrap clearfix">
<p class="left_btn"><a href="javascript:void(0)"><img src="images/btn.jpg" /></a></p>
<div id="scroll_img">
<ul class="scroll_img_list clearfix">
<li>
<p><a href="#"><img src="images/1.jpg" alt="#" /></a></p>
<p><a href="#">information1</a></p>
</li>
<li>
<p><a href="#"><img src="images/1.jpg" alt="#" /></a></p>
<p><a href="#">information2</a></p>
</li>
<li>
<p><a href="#"><img src="images/1.jpg" alt="#" /></a></p>
<p><a href="#">information3</a></p>
</li>
<li>
<p><a href="#"><img src="images/1.jpg" alt="#" /></a></p>
<p><a href="#">information4</a></p>
</li>
<li>
<p><a href="#"><img src="images/2.jpg" alt="#" /></a></p>
<p><a href="#">information5</a></p>
</li>
<li>
<p><a href="#"><img src="images/2.jpg" alt="#" /></a></p>
<p><a href="#">information6</a></p>
</li>
<li>
<p><a href="#"><img src="images/2.jpg" alt="#" /></a></p>
<p><a href="#">information7</a></p>
</li>
<li>
<p><a href="#"><img src="images/2.jpg" alt="#" /></a></p>
<p><a href="#">information8</a></p>
</li>
<li>
<p><a href="#"><img src="images/1.jpg" alt="#" /></a></p>
<p><a href="#">information9</a></p>
</li>
<li>
<p><a href="#"><img src="images/1.jpg" alt="#" /></a></p>
<p><a href="#">information10</a></p>
</li>
<li>
<p><a href="#"><img src="images/1.jpg" alt="#" /></a></p>
<p><a href="#">information11</a></p>
</li>
<li>
<p><a href="#"><img src="images/1.jpg" alt="#" /></a></p>
<p><a href="#">information12</a></p>
</li>
<li>
<p><a href="#"><img src="images/1.jpg" alt="#" /></a></p>
<p><a href="#">information13</a></p>
</li>
<li>
<p><a href="#"><img src="images/1.jpg" alt="#" /></a></p>
<p><a href="#">information14</a></p>
</li>
</ul>
</div>
<p class="right_btn"><a href="javascript:void(0)"><img src="images/btn.jpg" /></a></p>
</div>
</body>
<script type="text/javascript">
(function(){
var test=new scroll_img('scroll_img',150,590,140,10);
test.initialize();
$('.left_btn').click(function(){
test.stop();
test.left();
test.autoplay();
});
$('.right_btn').click(function(){
test.stop();
test.right();
test.autoplay();
});
})();
</script>
</html>
scroll_img.js如下:
function scroll_img(target,distance,show_width,li_width,mr){
this.distance=distance; //每次移動(dòng)距離
this.target=$("#"+target);
this.show_width=show_width; //顯示區(qū)域?qū)挾?
this.li_width=li_width; //items寬度
this.mr=mr; //items間距
this.scrollbar=$("#"+target).find('ul');
this.position=0;
this.direction=1;
}
scroll_img.prototype={
version:1.00,
author:"yangfeifei",
date:2011-11-21,
initialize:function(){
var t=this;
t.scrollbar.css('position','relative');
//初始動(dòng)作
t.autoplay();
t.scrollbar.mouseover(function(){t.stop();}); //鼠標(biāo)移到圖片上停止自動(dòng)播放
t.scrollbar.mouseout(function(){t.autoplay();}); //鼠標(biāo)移出圖片開始自動(dòng)播放
},
right:function(){
var t=this;
(-t.position)<t.total_length()?t.position-=t.distance:t.position=t.position;
if((-t.position)<t.total_length()){
t.scrollbar.animate({left:t.position},500);
t.direction=1;
}
if((-t.position)==t.total_length()){
t.scrollbar.animate({left:t.position},500);
t.direction=-1;
}
},
left:function(){
var t=this;
(-t.position)>0?t.position+=t.distance:t.position=t.position;
if((-t.position)>0){
t.scrollbar.animate({left:t.position},500);
t.direction=-1;
}
if((-t.position)==0){
t.scrollbar.animate({left:t.position},500);
t.direction=1;
}
},
total_length:function(){
var t=this,
total_num=t.scrollbar.find('li').length;
return total_num*(t.li_width+t.mr)-(t.mr+t.show_width);
},
autoplay:function(){
var t=this;
t.setInt=setInterval(function(){
t.direction==1?t.right():t.left();
},3000);
},
stop:function(){
var t=this;
clearInterval(t.setInt);
}
}
global.css如下:
/*CSS reset*/
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td{margin:0;padding:0;font-family:arial,"宋體";}
table{border-collapse:collapse;border-spacing:0;}
fieldset,img{border:0;}
address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:bold;}
ol,ul{list-style-type:none;}
caption,th{text-align:left;}
h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}/*font-size:100%;的作用主要是改變它默認(rèn)的大小,繼承父體的字體大小*/
q:before,q:after{content:' ';}
abbr,acronym{border:0;}
.cb{clear:both;}
.cl{clear:left;}
.cr{clear:right;}
.clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden;}
.clearfix{display:inline-block;}
* html .clearfix{height:1%;}
.clearfix{display:block;}
/*主體css*/
.scroll_img_wrap{width:612px;margin: 10px auto;}
#scroll_img{width:590px;overflow:hidden;float:left;*position:relative;border: 1px #ccc solid;}/*可視區(qū)域?qū)挾?/
.scroll_img_list{width:9999px;}
.scroll_img_list li{float:left;width:140px;margin-right:10px;}/*items*/
.scroll_img_list li p{text-align:center;}
.scroll_img_list li p a{text-decoration:none;color:#666;}
.left_btn,.right_btn{float:left;}
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
- Vue.js 無限滾動(dòng)列表性能優(yōu)化方案
- 原生JS實(shí)現(xiàn)列表內(nèi)容自動(dòng)向上滾動(dòng)效果
- JS實(shí)現(xiàn)的新聞列表自動(dòng)滾動(dòng)效果示例
- js實(shí)現(xiàn)文字列表無縫滾動(dòng)效果
- JS觸摸屏網(wǎng)頁版仿app彈窗型滾動(dòng)列表選擇器/日期選擇器
- 基于JS實(shí)現(xiàn)新聞列表無縫向上滾動(dòng)實(shí)例代碼
- JS實(shí)現(xiàn)超精簡的鏈接列表在固定區(qū)域內(nèi)滾動(dòng)效果代碼
- js實(shí)現(xiàn)Select列表內(nèi)容自動(dòng)滾動(dòng)效果代碼
- js實(shí)現(xiàn)列表向上無限滾動(dòng)
相關(guān)文章
支付寶小程序?qū)崿F(xiàn)類似微信多行輸入功能(思路詳解)
這篇文章主要介紹了支付寶小程序?qū)崿F(xiàn)類似微信多行輸入功能,輸入超過 8 行的時(shí)候會(huì)出現(xiàn)滾動(dòng),這樣做的好處就是輸入框不會(huì)直接頂?shù)巾撁孀铐敳?支付寶小程序?qū)崿F(xiàn)多行輸入框:使用textarea多行輸入框?qū)崿F(xiàn),感興趣的朋友一起看看吧2024-02-02
微信小程序?qū)W習(xí)筆記之本地?cái)?shù)據(jù)緩存功能詳解
這篇文章主要介紹了微信小程序?qū)W習(xí)筆記之本地?cái)?shù)據(jù)緩存功能,結(jié)合實(shí)例形式分析了微信小程序wx.setStorage、wx.getStorage以及wx.removeStorage、wx.clearStorage針對(duì)數(shù)據(jù)緩存的存取、刪除等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
javascript驗(yàn)證只能輸入數(shù)字和一個(gè)小數(shù)點(diǎn)示例
使用javascript限制只能輸入數(shù)字和一個(gè)小數(shù)點(diǎn),在某些情況下還是比較使用的,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下2013-10-10
javascript獲取select標(biāo)簽選中的值
這篇文章主要介紹javascript獲取select標(biāo)簽選中的值方法,比較實(shí)用,需要的朋友可以參考下。2016-06-06
微信小程序填寫用戶頭像和昵稱實(shí)現(xiàn)方法淺析
這篇文章主要介紹了微信小程序填寫用戶頭像和昵稱實(shí)現(xiàn)方法,我們使用小程序往往能碰到提示允許獲取用戶頭像昵稱,這種功能怎么實(shí)現(xiàn)呢?本篇文章帶你探索2023-02-02
JavaScript模塊規(guī)范之AMD規(guī)范和CMD規(guī)范
本篇文章給大家介紹js模塊規(guī)范之AMD規(guī)范和CMD規(guī)范,模塊化是一種處理復(fù)雜系統(tǒng)分解為代碼結(jié)構(gòu)更合理,可維護(hù)性更高的可管理的模塊的方式,對(duì)js amd cmd規(guī)范感興趣的童鞋可以來本文學(xué)習(xí)2015-10-10
擁有一個(gè)屬于自己的javascript表單驗(yàn)證插件
這篇文章主要幫助大家擁有一個(gè)屬于自己的javascript表單驗(yàn)證插件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03
詳解用函數(shù)式編程對(duì)JavaScript進(jìn)行斷舍離
本篇文章主要介紹了用函數(shù)式編程對(duì)JavaScript進(jìn)行斷舍離,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09

