本文實例講述了原生JS實現的簡單輪播圖功能。分享給大家供大家參考,具體如下:
經過幾天的努力,終于攻克了這一難題,于是迫不及待的想要分享給大家,編寫之前,我也看了不少其他博主的博客,大多是用偏移量寫的,對新手來說,還是有些難以理解,雖然可能實現的需求不一樣,但我想先從簡入手,所以自己查閱資料,修改bug,終于完成。話不多說,上代碼:
<!DOCTYPE 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"><title>m.survivalescaperooms.com JS輪播圖</title><script src="jquery.js"></script><style>*{margin: 0px;padding:0px;list-style: none;text-decoration: none;}#flash{ /*根據圖片的大小來設置外層div的大小*/width: 520px;height: 280px;margin: 0 auto;position: relative; /*設置div定位,方便之后圖片及圓點位置的設定*/border:1px solid black;}#flash img{width: 100%;height: 100%;position: absolute; /*設置所有圖片重疊*/left: 0px;top: 0px;display: none; /*設置所有圖片隱藏,通過改變第一個圖片的行間樣式來使第一個圖片顯示*/}#flash ul{width: 150px;height: 25px;border-radius: 20px;background-color:rgba(255,255,255,0.5);position: absolute;left: 35%;bottom: 10%;}#flash ul li{width: 12px;height: 12px;margin-top:5px;background-color: #fff;border-radius: 50%;margin-left: 15px;float: left;}#flash ul .li_1{background-color: #f40; /*設置第一個圓點背景色為紅色*/}#flash .span-r{width: 50px;height: 50px;border-radius: 50%;position: absolute;right: 2%;top: 45%;background-color: rgba(255,255,255,0.5);}#flash .span-r span{width: 100%;height:100%;color:rgba(0,0,0,0.5);font-size: xx-large;font-weight: 700;line-height: 50px;margin-left: 15px;cursor: pointer;}#flash .span-l{width: 50px;height: 50px;border-radius: 50%;position: absolute;left: 2%;top: 45%;background-color: rgba(255,255,255,0.5);}#flash .span-l span{width: 100%;height:100%;color:rgba(0,0,0,0.5);font-size: xx-large;font-weight: 700;line-height: 50px;margin-left: 15px;cursor: pointer;}</style></head><div id="flash"><img src="http://demo.vevb.com/js/2018/html5-css3-3d-img-flash-codes/images/Guardians-of-the-Galaxy-Poster-High-Res.jpg" src="/uploads/allimg/180819/104J544U-0.jpg" style="border: 1px solid rgb(204, 204, 204); vertical-align: middle; padding: 1px; overflow: hidden; max-width: 650px;" />	感興趣的朋友可以自己動手測試一下。
	希望本文所述對大家JavaScript程序設計有所幫助。
新聞熱點
疑難解答