當前位置:編程學習大全網 - 人物素材 - 如何dwcs5中讓圖片實現無限滾動

如何dwcs5中讓圖片實現無限滾動

首先,妳得準備好素材,下面的1.jpg~6.jpg是測試用的。

然後打開Dreamweaver8,新建壹網頁文件,並保存為名為“index.html"文件。

切換至代碼編輯界面,輸入如下代碼:

<body><div id="photo-list"> <ul id="scroll">

<li><a href="#"><img src="images/1.jpg" width="100px" height="100px" alt=""/></a></li>

<li><a href="#"><img src="images/2.jpg" width="100px" height="100px" alt=""/></a></li>

<li><a href="#"><img src="images/3.jpg" width="100px" height="100px" alt=""/></a></li>

<li><a href="#"><img src="images/4.jpg" width="100px" height="100px" alt=""/></a></li>

<li><a href="#"><img src="images/5.jpg" width="100px" height="100px" alt=""/></a></li>

<li><a href="#"><img src="images/6.jpg" width="100px" height="100px" alt=""/></a></li> </ul> </div></body>

新建壹CSS樣式表文件,並將該文件保存到與“index.html”相同的目錄下,文件名稱為“MyStyle.css"。

在新建的樣式表文件"MyStyle.css”文件中輸入如下代碼:

* { padding:0; margin:0;} /*設置所有對像的內邊距為0*/

body { text-align:center;} /*設置頁面居中對齊*/

#photo-list {

/* 6張圖片的寬度(包含寬度、padding、border、圖片間的留白)

計算:6*(100+2*2+1*2+9) - 9

之所以減去9是第6張圖片的右邊留白 */

width:681px;

/* 圖片的寬度(包含高度、padding、border)

計算:100+2*2+1*2 */

height:106px;

margin:50px auto;

overflow:hidden; /*溢出部份將被隱藏*/

border:1px dashed #ccc;

}

#photo-list ul { list-style:none;}

#photo-list li { float:left; padding-right:9px;}

#photo-list img { border:1px solid #ddd; background:#fff; padding:2px;}

在網頁文件"index.html"中添加對該樣式表的引用:

<link rel="stylesheet" type="text/css" href="MyStyle.css">

新建壹個JS文件,並將該文件另存為“MoveEffect.js"。

在”MoveEffect.js“文件中輸入如下所示代碼:

var id = function(el) { return document.getElementById(el); },

c = id('photo-list');

if(c) {

var ul = id('scroll'),

lis = ul.getElementsByTagName('li'),

itemCount = lis.length,

width = lis[0].offsetWidth, //獲得每個img容器的寬度

marquee = function() {

c.scrollLeft += 2;

if(c.scrollLeft % width <= 1){ //當 c.scrollLeft 和 width 相等時,把第壹個img追加到最後面

ul.appendChild(ul.getElementsByTagName('li')[0]);

c.scrollLeft = 0;

};

},

speed = 50; //數值越大越慢

ul.style.width = width*itemCount + 'px'; //加載完後設置容器長度

var timer = setInterval(marquee, speed);

c.onmouseover = function() {

clearInterval(timer);

};

c.onmouseout = function() {

timer = setInterval(marquee, speed);

};

};

然後在主頁文件"index.html”中添加對該“MoveEffect.js”文件的引用。

<script type="text/javascript" src="MoveEffect.js"></script>

打開“index.html”網頁文件

  • 上一篇:小朋友學文言文
  • 下一篇:支付寶錦鯉魚是什麽意思?支付寶錦鯉魚獎勵內容列表[多個]
  • copyright 2024編程學習大全網