JS圖片無縫滾動(dòng)(簡單利于使用)
原樣復(fù)制后,幾乎不需要改動(dòng)就能用了!有問題大家討論
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>滾動(dòng)代碼</title>
<style>
*{ margin:0; padding:0;}
ul,li{ list-style:none;}
.left_1{ width:500px; height:100px; margin:0 auto;}
.left_1 ul{}
.left_1 li{ float:left; width:120px; height:100px; line-height:100px; text-align:center;}
.left_1 p{ width:50px; he
100px; line-height:100px;}
.left_1_i div{ width:100px; height:100px; line-height:100px; border:1px solid #e5e5e5; margin-left:10px; padding-left:10px;}
</style>
<script language="javascript">
function scrollSZ(con_id,speed,direct){
var con,items,heightHalf,heightAll;
var timer;
speed = parseInt(speed);//獲取設(shè)置的速度參數(shù)
con = document.getElementById(con_id);
con.style.overflow = "hidden";
if(direct == "top"){
direct = "top";
}else if(direct == "bottom"){
direct = "bottom";
}else{
direct = "top";
}
con.innerHTML +=con.innerHTML;
con.innerHTML +=con.innerHTML;
items = getChildNodes(con);
if(items.length < 1){
return;
}
heightAll = 0;
for(var i=0;i<items.length;i++){
var numTop,numBottom;
if (!!window.ActiveXObject){
numTop = items[i].currentStyle["marginTop"];
numBottom = items[i].currentStyle["marginBottom"];
}else{
numTop = document.defaultView.getComputedStyle(items[i],null)["marginTop"];
numBottom = document.defaultView.getComputedStyle(items[i],null)["marginBottom"];
}
numTop = parseInt(numTop);
numBottom = parseInt(numBottom);
numTop += numBottom;
if(numTop >0){
heightAll += numTop;
}
heightAll += items[i].offsetHeight;
}
heightHalf = heightAll/2;
if(direct == "bottom"){
con.scrollTop = heightHalf;
timer = setInterval(_scrollBottom,speed);
}else if(direct == "top"){
timer = setInterval(_scrollTop,speed);
}
con.onmouseover = function(){
if(timer){
clearInterval(timer);
timer = null;
}
};
con.onmouseout = function(){
if(!timer){
if(direct == "top"){
timer = setInterval(_scrollTop,speed);
}else if(direct == "bottom"){
timer = setInterval(_scrollBottom,speed);
}
}
};
function _scrollTop(){
if(con.scrollTop < heightHalf){
con.scrollTop += 2;
}else{
con.scrollTop = 0;
}
}
function _scrollBottom(){
if(con.scrollTop > 0){
con.scrollTop -= 2;
}else{
con.scrollTop = heightHalf;
}
}
}
function scrollSP(con_id,speed,direct){
var con,innerCon,timer,items,widthAll,widthHalf;
speed = parseInt(speed);
con = document.getElementById(con_id);
con.style.overflow = "hidden";
items = getChildNodes(con);
if(items.length == 1){
innerCon = items[0];
}else{
return;
}
innerCon.innerHTML += innerCon.innerHTML;
innerCon.innerHTML += innerCon.innerHTML;
items = getChildNodes(innerCon);
if(items.length<1){
return;
}
widthAll = 0;
for(var i=0;i<items.length;i++){
}
for(var i=0;i<items.length;i++){
var numLeft,numRight;
if (!!window.ActiveXObject){
items[i].style.styleFloat = "left";
numLeft = items[i].currentStyle["marginLeft"];
numRight = items[i].currentStyle["marginRight"];
}else{
items[i].style.cssFloat = "left";
numLeft = document.defaultView.getComputedStyle(items[i],null)["marginLeft"];
numRight = document.defaultView.getComputedStyle(items[i],null)["marginRight"];
}
numLeft = parseInt(numLeft);
numRight = parseInt(numRight);
numLeft += numRight;
if(numLeft>0){
widthAll += numLeft;
}
widthAll += items[i].offsetWidth;
}
widthHalf = widthAll/2;
innerCon.style.width = widthAll+"px";
if(direct == "left"){
direct = "left";
}else if(direct == "right"){
direct = "right";
}else{
direct = "left"
}
if(direct == "left"){
timer = setInterval(_scrollLeft,speed);
}else if(direct == "right"){
con.scrollLeft = widthHalf;
timer = setInterval(_scrollRight,speed);
}
con.onmouseover = function(){
if(timer){
clearInterval(timer);
timer = null;
}
}
con.onmouseout = function(){
if(direct == "left"){
timer = setInterval(_scrollLeft,speed);
}else{
timer = setInterval(_scrollRight,speed);
}
}
function _scrollLeft(){
if(con.scrollLeft<widthHalf){
con.scrollLeft += 2;
}else{
con.scrollLeft = 0;
}
}
function _scrollRight(){
if(con.scrollLeft>0){
con.scrollLeft -= 2;
}else{
con.scrollLeft = widthHalf;
}
}
}
function getChildNodes(obj){//獲取元素子節(jié)點(diǎn)
var childList,list;
childList = new Array();
list = obj.childNodes;
for(var i=0;i<list.length;i++){
if(list[i].nodeType == 1)
childList[childList.length] = list[i];
}
return childList;
}
</script>
</head>
<body>
<div id="left_1" class="left_1">
<div class="left_1_i">
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<div>b</div>
<div>a</div>
</div>
</div>
<script language="javascript">scrollSP("left_1",10,"right");</script>
</body>
</html>
相關(guān)文章
基于JS實(shí)現(xiàn)checkbox全選功能實(shí)例代碼
最近做了個(gè)項(xiàng)目其中有這樣的需求要求實(shí)現(xiàn)點(diǎn)擊全選選中所有菜單,再次點(diǎn)擊全選取消選中。下面小編給大家分享實(shí)現(xiàn)代碼,對(duì)js實(shí)現(xiàn)checkbox全選功能感興趣的朋友參考下吧2016-10-10js canvas實(shí)現(xiàn)擦除效果示例代碼
擦除效果在我們?nèi)粘i_發(fā)中也是時(shí)有見到的,通過擦除效果大大加強(qiáng)了與用戶的交互性,所以下面這篇文章主要給大家介紹了利用js和canvas實(shí)現(xiàn)擦除效果的相關(guān)資料,文中給出了詳細(xì)的介紹和示例代碼,需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04JS常用倒計(jì)時(shí)代碼實(shí)例總結(jié)
這篇文章主要介紹了JS常用倒計(jì)時(shí)代碼,結(jié)合實(shí)例形式總結(jié)分析了JS常用的倒計(jì)時(shí)功能實(shí)現(xiàn)方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02JavaScript實(shí)現(xiàn)元素滾動(dòng)條到達(dá)一定位置循環(huán)追加內(nèi)容
下面小編就為大家分享一篇JavaScript實(shí)現(xiàn)元素滾動(dòng)條到達(dá)一定位置循環(huán)追加內(nèi)容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-12-12自己實(shí)現(xiàn)string的substring方法 人民幣小寫轉(zhuǎn)大寫,數(shù)字反轉(zhuǎn),正則優(yōu)化
這是最近碰到的幾個(gè)題目,簡單地寫一下我自己的方案,在此分享給大家,也希望能看到大家的方案2012-09-09