基于JavaScript實現(xiàn)簡單的輪播圖_第1頁
基于JavaScript實現(xiàn)簡單的輪播圖_第2頁
基于JavaScript實現(xiàn)簡單的輪播圖_第3頁
基于JavaScript實現(xiàn)簡單的輪播圖_第4頁
基于JavaScript實現(xiàn)簡單的輪播圖_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第基于JavaScript實現(xiàn)簡單的輪播圖本文實例為大家分享了JavaScript實現(xiàn)簡單的輪播圖的具體代碼,供大家參考,具體內容如下

js輪播圖實現(xiàn)思路

1、先獲取元素盒子左右按鈕

2、鼠標經過顯示/隱藏左右側按鈕

3、動態(tài)生成小圓圈、添加自定義屬性

4、小圓圈點擊事件添加current類名

5、添加動畫事件animate等于-索引號*focusWidth

6、克隆第一張圖片到最后面

7、添加右側/左側按鈕點擊事件

8、設置定時器手動調用右側按鈕點擊事件

html代碼部分

!DOCTYPEhtml

htmllang="en"

head

metacharset="UTF-8"

metahttp-equiv="X-UA-Compatible"content="IE=edge"

metaname="viewport"content="width=device-width,initial-scale=1.0"

titleDocument/title

linkrel="stylesheet"href="css/index.css"

scriptsrc="js/index.js"/script

/head

body

div

ahref="javascript:;"/a

ahref="javascript:;"

ahref=""imgsrc="images/focus.jpg"alt=""/a

/li

ahref=""imgsrc="images/focus1.jpg"alt=""/a

/li

ahref=""imgsrc="images/focus2.jpg"alt=""/a

/li

ahref=""imgsrc="images/focus3.jpg"alt=""/a

/li

/ul

/ol

/div

/body

/html

css樣式部分

padding:0;

margin:0;

li{

list-style:none;

img{

border:0;

vertical-align:top;

text-decoration:none;

.focus{

position:relative;

width:721px;

height:455px;

margin:100pxauto;

overflow:hidden;

.focusul{

position:absolute;

top:0;

left:0;

width:600%;

.focusulli{

float:left;

.arrow_r,

.arrow_l{

display:none;

position:absolute;

top:50%;

transform:translateY(-50%);

width:40px;

height:40px;

line-height:40px;

text-align:center;

font-size:24px;

background:rgba(0,0,0,.2);

color:#fff;

z-index:1;

.arrow_r{

right:0;

.circle{

position:absolute;

bottom:10px;

left:50px;

.circleli{

float:left;

width:8px;

height:8px;

border:2pxsolidrgba(255,255,255,.5);

border-radius:50%;

margin:03px;

cursor:pointer;

.current{

background-color:#fff;

}

javascript部分

window.addEventListener('load',function(){

//獲取元素

varfocus=document.querySelector('.focus');

vararrow_r=document.querySelector('.arrow_r');

vararrow_l=document.querySelector('.arrow_l');

varfocusWidth=focus.offsetWidth;

//鼠標經過focus盒子顯示/隱藏左右側按鈕暫停輪播

focus.addEventListener('mouseenter',function(){

arrow_r.style.display='block';

arrow_l.style.display='block';

clearInterval(timer);

timer=null;

focus.addEventListener('mouseleave',function(){

arrow_r.style.display='none';

arrow_l.style.display='none';

timer=setInterval(function(){

//調用點擊事件

arrow_r.click();

},2000);

//動態(tài)生成小圓圈

varul=focus.querySelector('ul');

varol=focus.querySelector('.circle');

for(vari=0;iul.children.length;i++){

varli=document.createElement('li');

li.setAttribute('index',i);

ol.appendChild(li);

//小圓圈點擊事件

li.addEventListener('click',function(){

for(vari=0;iol.children.length;i++){

ol.children[i].className='';

varindex=this.getAttribute('index');

num=index;

circle=index;

this.className='current';

animate(ul,-index*focusWidth);

ol.children[0].className='current';

//克隆第一張圖片放到最后一張

varfis=ul.children[0].cloneNode(true);

ul.appendChild(fis);

//右側按鈕點擊事件

varnum=0;

varcircle=0;

arrow_r.addEventListener('click',function(){

if(num===ul.children.length-1){

ul.style.left=0;

num=0;

num++;

animate(ul,-num*focusWidth);

circle++;

if(circle===ul.children.length-1){

circle=0;

for(vari=0;iol.children.length;i++){

ol.children[i].className='';

ol.children[circle].className='current';

//左側按鈕點擊事件

arrow_l.addEventListener('click',function(){

if(num==0){

num=ul.children.length-1;

ul.style.left=-num*focusWidth+'px';

num--;

animate(ul,-num*focusWidth);

circle--;

circle=circle0ol.children.length-1:circle;

//調用函數(shù)

circleChange();

//清除其余小圓圈的current類名

functioncircleChange(){

for(vari=0;iol.children.length;i++){

ol.children[i].className='';

//留下當前的小圓圈的current類名

ol.children[circle].className='current';

//動畫函數(shù)

functionanimate(obj,target,callback){

clearInterval(obj.timer);

obj.timer=setInterval(function(){

varstep=(target-obj.offsetLeft)/10;

step=step0Math.ceil(step):Math.floor(step);

if(obj.offsetLeft==target){

clearInterval(obj.t

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論