使用html+js+css 實(shí)現(xiàn)頁面輪播圖效果(實(shí)例講解)
更新時(shí)間:2017年09月21日 08:40:09 作者:歡呀
下面小編就為大家?guī)硪黄褂胔tml+js+css 實(shí)現(xiàn)頁面輪播圖效果(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
html 頁面
<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="carousel.css" rel="external nofollow" > <title>輪播圖效果</title> </head> <body> <section id="main"> <div id="picture"></div> <!-- 添加圖中按鈕 圖片輪播在js中大致成型后再寫最好--> <div id="dot"> <span></span> <span></span> <span></span> <span></span> <span></span> </div> <!-- 添加切換按鈕 --> <div id="an"> <div class="left"><</div> <div class="right">></div> </div> </section> <script src="jquery.js"></script> <script src="carousel.js"></script> </body>
css頁面 carousel.css
#main{
width: 655px;
height: 361px;
position: relative;
}
#picture{
width:100%;
height: 100%;
}
#picture img{
width:100%;
height: 100%;
display: none;
}
#picture img:nth-child(1){
display: inline-block;
}
/* 設(shè)置圓點(diǎn)的樣式 */
#dot span{
display: inline-block;
width:25px;
height: 25px;
border-radius: 50%;
background-color: gray;
margin-left: 10px;
opacity: 0.6
}
#dot{
position: absolute;
right: 40px;
bottom: 30px;
}
/* 設(shè)置頁面剛加載的圓點(diǎn)初始狀態(tài) 1 第一個(gè)圓點(diǎn)放大1.2倍 2、顏色變成藍(lán)色
*/
#dot :nth-of-type(1){
transform: scale(1.2);
background-color: blue;
}
.left ,.right{
width: 40px;
height: 40px;
border-radius: 50%;
font-size: 30px;
color: white;
position: absolute;
bottom: calc((100% - 40px)/2);
text-align: center;
}
.left{
left: 15px;
}
.right{
right: 15px;
}
.left:hover ,.right:hover{
background-color: white;
color:red;
}
js頁面 carousel.js
for(var i=1; i<6;i++){
$('#picture').append("<img src='./../images/"+i+".jpg' >");
}
//給圖片轉(zhuǎn)化設(shè)置定時(shí)函數(shù)
var index=0;
var timer;
function changeImageDot(){
$('#picture img:nth-child('+(index+1)+')').css({
display:'block',
}).siblings().css({
display:'none',
});
//設(shè)置隨圖片切換,對(duì)應(yīng)的圓點(diǎn)樣式發(fā)生變化
// index+1 是因?yàn)樗饕菑?開始而 nth-child(i) 中的i是從1 開始的
$('#dot span:nth-child('+(index+1)+')').css({
transform: 'scale(1.2)',
'background-color': 'blue',
}).siblings().css({
transform: 'scale(1)',
'background-color':'gray',
});
}
function produceTime(){
timer=setInterval(function(){
index++;
if(index==5)
index=0;
changeImageDot();
},2000);
}
produceTime();
//鼠標(biāo)懸浮在圓點(diǎn)上 , 圓點(diǎn)和圖片的變化
$('#dot span').mouseenter(function(){
index=$(this).index();
changeImageDot();
clearInterval(timer);
produceTime();
});
//缺點(diǎn):點(diǎn)擊切換按鈕后,圖片切換后 ,會(huì)立即切換到下一個(gè)圖片,需要設(shè)置 清除計(jì)時(shí)器后再新建一個(gè)計(jì)時(shí)器
$('.left').click(function(){
index--;
if(index==-1)
index=4;
changeImageDot();
clearInterval(timer);
produceTime();
});
$('.right').click(function(){
index++;
if(index==5)
index=0;
changeImageDot();
clearInterval(timer);
produceTime();
以上這篇使用html+js+css 實(shí)現(xiàn)頁面輪播圖效果(實(shí)例講解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript暫停和繼續(xù)定時(shí)器的實(shí)現(xiàn)方法
這篇文章主要介紹了JavaScript暫停和繼續(xù)定時(shí)器的方法的相關(guān)資料,非常不錯(cuò),需要的朋友可以參考下2016-07-07
javascript下高性能字符串連接StringBuffer類
使用StringBuffer類比使用加號(hào)節(jié)省50%左右的時(shí)間,大家對(duì)于大數(shù)據(jù)的連接最好使用這個(gè)方法。2010-08-08
JS變量中有var定義和無var定義的區(qū)別以及es6中l(wèi)et命令和const命令
這篇文章主要介紹了JS變量中有var定義和無var定義的區(qū)別以及es6中l(wèi)et命令和const命令,需要的朋友可以參考下2017-02-02
JavaScript Event學(xué)習(xí)第九章 鼠標(biāo)事件
鼠標(biāo)事件是到目前為止最重要的事件。在這一章我將介紹一些鼠標(biāo)事件的最常見的問題和技巧。2010-02-02

