jquery輪播圖的實現
阿新 • • 發佈:2020-12-08
今天分享的是一個簡單的輪播圖,這個輪播圖的特效很簡單,能夠進行圖片的輪播以及點選相應圖片,圖片能夠跳轉到相應位置,首先書寫的div部分:
<div id="scrollPics">
<ul class="slider">
<li><img src="../images/1.jpg" alt=""></li>
<li><img src="../images/2.jpg" alt=""></li>
<li><img src="../images/3.jpg" alt=""></li>
<li><img src="../images/7.jpg" alt=""></li>
<li><img src="../images/5.jpg" alt=""></li>
</ul>
<ul class="num"></ul>
</div>
然後書寫style樣式部分
<style>
*{
margin: 0px;
padding: 0px;
}
ul{
list-style: none;
}
#scrollPics{
height: 420px;
width: 790px;
margin-bottom: 10px;
overflow: hidden;
position: relative;
top: 100px;
left:400px;
}
.slider{
margin-top: 0px;
}
.slider img{
width: 100%;
}
.num{
position: absolute;
right: 5px;
bottom: 5px;
}
.num li{
float: left;
color: #ff7300;
text-align: center;
line-height: 16px;
width: 16px;
height: 16px;
cursor: pointer;
overflow: hidden;
margin: 3px 1px;
border: 1px solid #ff7300;
background-color: white;
border-radius: 50%;
}
.num li.active{
color: #fff;
line-height: 21px;
width: 21px;
height: 21px;
font-size: 16px;
margin: 0 1px;
border: 0;
background-color: #ff7300;
font-weight: bold;
border-radius: 50%;
cursor: pointer;
}
</style>
最後是script部分
<script>
$(function () {
var slider =$("#scrollPics .slider");
//獲取圖片
var imgCon =$("#scrollPics .slider li");
//除第一張其餘的圖片全部隱藏
imgCon.not(imgCon.eq(0)).hide();
//定義頁碼
var num =$("#scrollPics .num")
//獲取li標籤的長度
//find()方法返回備選元素的後代元素
var len =slider.find("li").length;
var html_page ="";
index=0;
//新增頁碼
for (var i=0;i<len;i++){
if (i===0){
html_page+="<li class='active'>"+(i+1)+"</li>"
}
else {
html_page +="<li>"+(i+1)+"</li>"
}
}
//輸出原點
num.html(html_page)
//顯示索引對應的圖片
function showPic(index) {
imgCon.eq(index).show().siblings("li").hide();
num.find("li").eq(index).addClass("active").siblings("li").removeClass("active")
}
//原點點選事件
$(".num li").click(function () {
index=$(this).index()
showPic(index)
})
//圖片輪播
$("#scrollPics").hover(function () {
clearInterval(window.timer)
},function () {
window.timer =setInterval(function () {
showPic(index);
index++;
if (index ===len){
index =0
}
},2000)
}).trigger("mouseleave")//觸發備選元素的指定事件
})
</script>
最後因為是用jquery書寫的程式碼,還要匯入 <script src="../js/jquery-2.2.3.js"></script>
廣州品牌設計公司https://www.houdianzi.com PPT模板下載大全https://redbox.wode007.com
最後是完整程式碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JQ輪播圖</title>
<script src="../js/jquery-2.2.3.js"></script>
<style>
*{
margin: 0px;
padding: 0px;
}
ul{
list-style: none;
}
#scrollPics{
height: 420px;
width: 790px;
margin-bottom: 10px;
overflow: hidden;
position: relative;
top: 100px;
left:400px;
}
.slider{
margin-top: 0px;
}
.slider img{
width: 100%;
}
.num{
position: absolute;
right: 5px;
bottom: 5px;
}
.num li{
float: left;
color: #ff7300;
text-align: center;
line-height: 16px;
width: 16px;
height: 16px;
cursor: pointer;
overflow: hidden;
margin: 3px 1px;
border: 1px solid #ff7300;
background-color: white;
border-radius: 50%;
}
.num li.active{
color: #fff;
line-height: 21px;
width: 21px;
height: 21px;
font-size: 16px;
margin: 0 1px;
border: 0;
background-color: #ff7300;
font-weight: bold;
border-radius: 50%;
cursor: pointer;
}
</style>
</head>
<body>
<div id="scrollPics">
<ul class="slider">
<li><img src="../images/1.jpg" alt=""></li>
<li><img src="../images/2.jpg" alt=""></li>
<li><img src="../images/3.jpg" alt=""></li>
<li><img src="../images/7.jpg" alt=""></li>
<li><img src="../images/5.jpg" alt=""></li>
</ul>
<ul class="num"></ul>
</div>
<script>
$(function () {
var slider =$("#scrollPics .slider");
//獲取圖片
var imgCon =$("#scrollPics .slider li");
//除第一張其餘的圖片全部隱藏
imgCon.not(imgCon.eq(0)).hide();
//定義頁碼
var num =$("#scrollPics .num")
//獲取li標籤的長度
//find()方法返回備選元素的後代元素
var len =slider.find("li").length;
var html_page ="";
index=0;
//新增頁碼
for (var i=0;i<len;i++){
if (i===0){
html_page+="<li class='active'>"+(i+1)+"</li>"
}
else {
html_page +="<li>"+(i+1)+"</li>"
}
}
//輸出原點
num.html(html_page)
//顯示索引對應的圖片
function showPic(index) {
imgCon.eq(index).show().siblings("li").hide();
num.find("li").eq(index).addClass("active").siblings("li").removeClass("active")
}
//原點點選事件
$(".num li").click(function () {
index=$(this).index()
showPic(index)
})
//圖片輪播
$("#scrollPics").hover(function () {
clearInterval(window.timer)
},function () {
window.timer =setInterval(function () {
showPic(index);
index++;
if (index ===len){
index =