分三个部分:
js 调用
css 样式
div部分
代码如下:- <!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=utf-8" />
- <title>div+css+JS图片滚动代码(无缝、平滑)</title>
- </head>
- <body>
- <style type="text/css">
- <!--
- .scroll_div {
- width:600px;
- height:80px;
- margin:0 auto;
- overflow: hidden;
- white-space: nowrap;
- background:#ccc;
- margin-left:5px;}
- .scroll_div img {
- width:80px;
- height:60px;
- border: 1px solid #D6D5DA;
- border:1px #efefef solid;
- margin-right: 12px;
- padding: 5px;
- clear: both;
- }
- #scroll_begin, #scroll_end, #scroll_begin ul, #scroll_end ul, #scroll_begin ul li, #scroll_end ul li{display:inline;} /*设置ul和li横排*/
- -->
- </style>
- <!--#####滚动区域#####-->
- <div id="scroll_div" class="scroll_div">
- <div id="scroll_begin">
- <ul>
- <li><a href="#"><img src="images/gy-img01.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img02.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img03.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img04.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img05.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img06.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img07.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img08.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img01.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img02.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img03.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img04.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img05.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img06.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img07.jpg" /></a></li>
- <li><a href="#"><img src="images/gy-img08.jpg" /></a></li>
- </ul>
- </div>
- <div id="scroll_end"></div>
- </div>
- <!--#####滚动区域#####-->
- <script>
- <!--
- var speed=10
- var scroll_begin = document.getElementById("scroll_begin");
- var scroll_end = document.getElementById("scroll_end");
- var scroll_div = document.getElementById("scroll_div");
- scroll_end.innerHTML=scroll_begin.innerHTML
- function Marquee(){
- if(scroll_end.offsetWidth-scroll_div.scrollLeft<=0)
- scroll_div.scrollLeft-=scroll_begin.offsetWidth
- else
- scroll_div.scrollLeft++
- }
- var MyMar=setInterval(Marquee,speed)
- scroll_div.onmouseover=function() {clearInterval(MyMar);}
- scroll_div.onmouseout=function() {MyMar=setInterval(Marquee,speed);}
- -->
- </script>
- </body>
- </html>
复制代码 然后刷新页面就可以用了…… 效果可以自己改~!
注:
white-space: nowrap; 规定此div中的元素(图片、文本)不进行换行。
display:inline; 规定此元素为行内元素显示,即此元素自动横排,不会另起一行显示。
这段CCS代码非常重要,不然框内的图片不能横排,即图片为竖排,然后不滚动图片。
相关链接:
http://bbs.phpcms.cn/forum.php?mod=viewthread&tid=92264 |