JavaScript實(shí)現(xiàn)簡(jiǎn)單圖片輪播效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)簡(jiǎn)單圖片輪播的具體代碼,最終實(shí)現(xiàn)效果圖

代碼塊
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
* {
margin: 0;
padding: 0
}
ul {
list-style: none
}
img {
vertical-align: top
}
.box {
width: 490px;
height: 170px;
margin: 100px auto;
padding: 5px;
border: 1px solid #ccc;
}
.inner {
width: 490px;
height: 170px;
background-color: pink;
overflow: hidden;
position: relative;
}
.inner ul {
width: 1000%;
position: absolute;
top: 0;
left: 0;
}
.inner li {
float: left;
}
.square {
position: absolute;
right: 10px;
bottom: 10px;
}
.square span {
display: inline-block;
width: 16px;
height: 16px;
background-color: #fff;
text-align: center;
line-height: 16px;
cursor: pointer;
}
.square span.current {
background-color: orangered;
color: #fff;
}
</style>
</head>
<body>
<div class="box" id="box">
<div class="inner">
<ul>
<li><a href="#"><img src="images/01.jpg" /></a></li>
<li><a href="#"><img src="images/02.jpg" /></a></li>
<li><a href="#"><img src="images/03.jpg" /></a></li>
<li><a href="#"><img src="images/04.jpg" /></a></li>
<li><a href="#"><img src="images/05.jpg" /></a></li>
</ul>
<div class="square">
<span class="current">1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
</div>
</div>
<script type="text/javascript">
//鼠標(biāo)經(jīng)過(guò)按鈕 按鈕排他
var box = document.getElementById("box");
var inner = box.children[0]; //獲取box下的第一個(gè)元素,也就是inner
var ul = inner.children[0]; //獲取inner下的ul
var squareList = inner.children[1]; //獲取inner下的第二個(gè)元素
var squares = squareList.children; //獲取所有的按鈕
var imgWidth = inner.offsetWidth;
// alert(imgWidth);
//給每個(gè)按鈕注冊(cè)鼠標(biāo)經(jīng)過(guò)事件
for(var i=0; i<squares.length; i++){
squares[i].index = i; //把索引保存在自定義屬性中
squares[i].onmouseover = function(){ //鼠標(biāo)經(jīng)過(guò)事件
//排他 干掉所有人
for(var j=0; j<squares.length; j++){
squares[j].className = "";
}
//留下我自己
this.className = "current";
//以動(dòng)畫的方式把ul移動(dòng)到指定的位置
//目標(biāo) 和當(dāng)前按鈕索引有關(guān),和圖片寬度有關(guān) 而且是負(fù)數(shù)
var target = -this.index * imgWidth; //獲取到索引
animate(ul,target);
}
}
function animate(obj, target) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
var step = 20;
var step = obj.offsetLeft < target ? step : -step;
if (Math.abs(obj.offsetLeft - target) > Math.abs(step)) {
obj.style.left = obj.offsetLeft + step + "px";
} else {
obj.style.left = target + "px";
clearInterval(obj.timer);
}
}, 15)
}
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js實(shí)現(xiàn)圖片無(wú)縫循環(huán)輪播
- JS實(shí)現(xiàn)圖片輪播效果實(shí)例詳解【可自動(dòng)和手動(dòng)】
- JS實(shí)現(xiàn)分頁(yè)瀏覽橫向圖片(類輪播)實(shí)例代碼
- 實(shí)現(xiàn)圖片首尾平滑輪播(JS原生方法—節(jié)流)
- JS 實(shí)現(xiàn)banner圖片輪播效果(鼠標(biāo)事件)
- 一個(gè)簡(jiǎn)易的js圖片輪播效果
- 利用vueJs實(shí)現(xiàn)圖片輪播實(shí)例代碼
- JS組件庫(kù)AlloyTouch實(shí)現(xiàn)圖片輪播過(guò)程解析
相關(guān)文章
Bootstrap實(shí)現(xiàn)登錄校驗(yàn)表單(帶驗(yàn)證碼)
本文給大家介紹使用Bootstrap新制作的一個(gè)登錄框,帶驗(yàn)證碼,帶校驗(yàn),非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友可以參考下2016-06-06
JavaScript實(shí)現(xiàn)前端實(shí)時(shí)搜索功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)前端實(shí)時(shí)搜索功能 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
JavaScript惰性求值的一種實(shí)現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于JavaScript惰性求值的一種實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
基于JS實(shí)現(xiàn)仿百度百家主頁(yè)的輪播圖效果
本文給大家分享基于html和js實(shí)現(xiàn)的仿百度百家主頁(yè)的輪播圖效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-03-03
Bootstrap的Refresh Icon也spin起來(lái)
本文通過(guò)實(shí)例給大家介紹Bootstrap的Refresh Icon相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-07-07
js判斷數(shù)組是否包含某個(gè)字符串變量的實(shí)例
下面小編就為大家分享一篇js判斷數(shù)組是否包含某個(gè)字符串變量的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-11-11
JavaScript中子函數(shù)訪問(wèn)外部變量的3種解決方法
任何在函數(shù)中定義的變量,都可認(rèn)為是私有變量,因?yàn)椴荒茉诤瘮?shù)外部訪問(wèn)這些變量,這篇文章主要給大家介紹了關(guān)于JavaScript中子函數(shù)訪問(wèn)外部變量的3種解決方法,需要的朋友可以參考下2021-06-06

