javascript实现图片无缝滚动(scrollLeft的使用方法介绍)
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style>
.piczhanshi{width:976px;height:167px;border:1px #999999 solid;margin:0 auto;}
h3{
float:left;
background-color: #DDD;
float: left;
height: 35px;
width: 966px;
margin-top: 2px;
line-height: 35px;
text-align: center;
color: #064278;
font-size: 18px;
margin-left: 5px;
margin-right: 5px;
margin-bottom: 10px;
}
.tupian{
float:left;
width:966px;
height:120px;
margin-left:5px;margin-right:5px;
}
.demo{width:966px;height:110px;overflow:hidden;margin-top:5px;margin-bottom:5px;} //这个是scrollLeft作用的层,实现移动效果的是他的子级divindexmo
#indemo{width:800%;height:110px;} //这里的宽度为他的父级div层的8倍宽
.demo1,.demo2{float:left;}
</style>
</head>
<body>
<div id="piczhanshi" class="piczhanshi">
<h3>鸟巢效果图</h3>
<div id="tupian" class="tupian">//这里是包括图片的最外层div是为了定位而设置的与滚动没关系
<div id="demo" class="demo"> // 这里是包括图片的div,也就是须要实现子级div滚动的层(scrollLeft是相对于当前层的子层的),这里必须这是好宽高已经overflow:hidden
<div id="indemo"> //这个是须要滚动的图片包括层,他的宽度在图片左右滚动效果下,比他的父级div的宽大,这里设置为800%,也就是相当他的父级div来说,他等于父级div宽的8倍
<div id="demo1" class="demo1">//这里是包括图片的层,通过“float”和demo2实现无缝连接
<img src="image/niaochao1.jpg" />
<img src="image/niaochao2.jpg" />
<img src="image/niaochao3.jpg" />
<img src="image/niaochao4.jpg" />
<img src="image/niaochao5.jpg" />
<img src="image/niaochao6.jpg" />
<img src="image/niaochao7.jpg" />
<img src="image/niaochao8.jpg" />
<img src="image/niaochao9.jpg" />
</div>
<div id="demo2" class="demo2"></div>//这里通过js代码“ tab2.innerHTML=tab1.innerHTML;”得到demo1的值,通过“float”和demo1实现无缝连接
</div>
</div>
</div>
</div>
<script language="javascript">
var speed=20;
var tab=document.getElementById("demo");
var tab1=document.getElementById("demo1");
var tab2=document.getElementById("demo2");
tab2.innerHTML=tab1.innerHTML; //通过赋值获得tab1的值
function marquee(){
if(tab2.offsetWidth-tab.scrollLeft<=0)//当tab的移动宽度大于等于demo1的时候,scrollLeft为0,也就是恢复到未移动前的情况,继续移动
{tab.scrollLeft=0}
else
{tab.scrollLeft++;} //图片层不断的往left移动
}
var timer=setInterval(marquee,speed); //定时器
tab.onmouseover=function(){clearInterval(timer)};//鼠标经过容器的时候清除定时器
tab.onmouseout=function(){timer=setInterval(marquee,speed);}//鼠标移开容器的时候開始定时器
</script>
</body>
</html>
*******************2004-04-26,优化后,点击左右的图片就清除自己主动滚动的定时器,实现点击的时候能够滚动对应的像素************************
<!DOCTYPE html >
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style>
.main{width:980px;height:190px;border:1px #999999 solid;margin:0 auto;}
h3{width:980px;height:30px;text-align:center;color:#3399FF;float:left;}
.tupian{width:980px;height:139px;float:left;position:relative;}
.indemo{width:800%;height:139px;}
.demo{width:948px;height:160px;float:left;overflow:hidden;}
.demo1{float:left;}
.demo2{float:left;}
.right{height:89px;width:16px;left:0;margin-top:40px;float:left;}
.left{height:89px;width:16px;margin-top:40px;float:left;}
</style>
</head>
<body>
<div id="main" class="main">
<h3>鸟巢展示图</h3>
<div id="tupian" class="tupian">
<div class="left"><img src="images/but_l2.jpg" class="lt" id="lt" /></div>
<div id="demo" class="demo">
<div id="indemo" class="indemo">
<div id="demo1" class="demo1">
<img src="images/niaochao1.jpg" />
<img src="images/niaochao2.jpg" />
<img src="images/niaochao3.jpg" />
<img src="images/niaochao4.jpg" />
<img src="images/niaochao5.jpg" />
<img src="images/niaochao6.jpg" />
<img src="images/niaochao7.jpg" />
<img src="images/niaochao8.jpg" />
<img src="images/niaochao9.jpg" />
</div>
<div id="demo2" class="demo2"></div>
</div>
</div>
<div class="right"><img src="images/but_r2.jpg" class="rt" id="rt" /></div>
</div>
</div>
<script type="text/javascript">
var speed=20;
var tab=document.getElementById("demo");
var tab1=document.getElementById("demo1");
var tab2=document.getElementById("demo2");
var lt=document.getElementById("lt");
var rt=document.getElementById("rt");
tab2.innerHTML=tab1.innerHTML;
function marquee(){
if(tab2.offsetWidth-tab.scrollLeft<=0)
{tab.scrollLeft=0}
else
{tab.scrollLeft++;}
}
var timer=setInterval(marquee,speed);
tab.onmouseover=function(){clearInterval(timer)};
tab.onmouseout=function(){timer=setInterval(marquee,speed)}
rt.onmouseout=function(){timer=setInterval(marquee,speed)};
//由于设置了点击图标的的时候清除定时器,所以这里必须在移开鼠标的时候開始定时器
lt.onmouseout=function(){timer=setInterval(marquee,speed)};
lt.onclick=function(){ //点击左图标时候执行
clearInterval(timer); //清除定时器
if(tab.scrollLeft>=1440){tab.scrollLeft=0;} //当层往左移动的像素大于1440的时候,scrillLeft返回0
else{tab.scrollLeft+=160} //不断的往左滚
}
rt.onclick=function(){ //点击右边鼠标的时候
clearInterval(timer);//清除定时器
if(tab.scrollLeft==0){tab.scrollLeft=1440;} //scroll为0的时候(这里必须注意,if里面是用“==”,是对照的意思,{}里面的“=”是赋值的意思,不能弄混),scrollLeft返回0
else if(tab.scrollLeft<=-1440){tab.scrollLeft=0;}//当scrollLeft少于1440的时候,scrollLeft返回0,同一时候)if(){} else{}之间是不存在不论什么标记符号的,这是格式
else{tab.scrollLeft-=160;}
}
</script>
</body>
</html>
javascript实现图片无缝滚动(scrollLeft的使用方法介绍)的更多相关文章
- Javascript实现图片无缝滚动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JavaScript学习笔记5 之 计时器 & scroll、offset、client系列属性 & 图片无缝滚动
一.计时器 setInterval ( 函数/名称 , 毫秒数 )表示每经过一定的毫秒后,执行一次相应的函数(重复) setTimeout ( 函数/名称 , 毫秒数 ) 表示经过一定的毫秒后,只执行 ...
- 应用JavaScript搭建一个简易页面图片无缝滚动效果
页面图片无缝滚动JavaScript原理:移动的区块包含图片内容,区块相对父级元素进行定位脱离文档流.再令区块的left值每隔固定的时间进行等量减少(或增大)从而实现区块的匀速运动.由于每次间隔移动的 ...
- jQuery图片无缝滚动JS代码ul/li结构
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jQuery图片无缝滚动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jquery图片无缝滚动代码左右 上下无缝滚动图片
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CSS和jQuery分别实现图片无缝滚动效果
一.效果图 二.使用CSS实现 <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...
- 原生javascript效果:无缝滚动
<style type="text/css"> #con {width:400px; padding:10px; margin:20px auto; text-alig ...
- Javascript获取图片原始宽度和高度的方法详解
前言 网上关于利用Javascript获取图片原始宽度和高度的方法有很多,本文将再次给大家谈谈这个问题,或许会对一些人能有所帮助. 方法详解 页面中的img元素,想要获取它的原始尺寸,以宽度为例,可能 ...
随机推荐
- 前端SEO优化
结构优化 1.扁平化结构,目录层次越少越好
- QVector 和vector的比较(QVector默认使用隐式共享,而且有更多的函数提供)
QVector和vector的比较: Qvector默认使用隐式共享,可以用setSharable改变其隐式共享.使用non-const操作和函数将引起深拷贝.at()比operator[](),快, ...
- JVM调优总结(十二)-参考资料
能整理出上面一些东西,也是因为站在巨人的肩上.下面是一些参考资料,供大家学习,大家有更好的,可以继续完善:) · Java 理论与实践: 垃圾收集简史 · Java SE 6 HotSpot[tm] ...
- poll系统调用的内核态实现机制分析
版权所有,转载请标明出处 All right reserved,Copyright by 徐行而至 浅唱而归 前面已经比较详尽的分析了系统调用引发的内核执行过程,本文将继续分析一下linux2.6 ...
- 蓝桥杯java高职组
标题1: 猜年龄 美国数学家维纳(N.Wiener)智力早熟,11岁就上了大学.他曾在1935~1936年应邀来中 国清华大学讲学. 一次,他参加某个重要会议,年轻的脸孔引人注目.于是 ...
- opencv分水岭算法对图像进行切割
先看效果 说明 使用分水岭算法对图像进行切割,设置一个标记图像能达到比較好的效果,还能防止过度切割. 1.这里首先对阈值化的二值图像进行腐蚀,去掉小的白色区域,得到图像的前景区域.并对前景区域用255 ...
- android中获取 bitmap 像素的颜色 之吸管取色功能
本功能是参考android API colorPickerView修改,实现类似与PS中吸管取色功能.也就是可以对图片的任意位置取该位置的RGB.本demo中,完成了色盘取色功能.当点击色盘的某个位置 ...
- jqGrid笔记@简单实现
jqGrid在MVC中的版本是已经通过 HtmlHelper 的扩展方法封装后的产物,webForm版本貌似也将其封装成了服务器端空间,所以我推荐下载原生的jqGrid版本的地址为:http://ww ...
- SQL Server 2008中文企业版下载地址和序列号[转]
SQLSERVER2008下载链接http://sqlserver.dlservice.microsoft.com/dl/download/B/8/0/B808AF59-7619-4A71-A447- ...
- Bee Framework_百度百科
Bee Framework_百度百科 Bee Framework 编辑 目录 1详细信息 简介 特性 2工作 主要模块 编译要求 运行要求 目录结构 运行例程 安装步骤 1详细信息 简介 ...