我需要弄清楚如何增加和減少計數器變數,以便當用戶單擊右箭頭按鈕時,影像將轉到第二個影像(#image2)然后是第三個影像(#image3),當用戶單擊左箭頭按鈕影像將回傳到上一個影像。我是 jQuery 的新手,也是使用計數器的新手。
$(document).ready(function() {
let counter = 1;
$("#image2").hide();
$("#image3").hide();
$("body").on('click',"#image2-thumbnail",function() {
currentimage = "#image2" counter;
counter ;
$(currentimage).hide();
$("#image1").slideUp('slow');
$("#image3").slideUp('slow');
$("#image2").slideDown('slow');
});
$("body").on('click',"#image3-thumbnail",function() {
currentimage = "#image2" counter;
counter ;
$(currentimage).hide();
$("#image2").fadeOut(1000);
$("#image1").fadeOut(1000);
$("#image3").fadeIn(1000);
});
$("body").on('click',"#image1-thumbnail",function() {
currentimage = "#image2" counter;
counter ;
$(currentimage).hide();
$("#image3").fadeOut(1000);
$("#image2").fadeOut(1000);
$("#image1").fadeIn(1000);
});
$("#rightArrow").click(function() {
currentimage = "#image2" counter;
counter ;
$(currentimage).hide();
}
)
$("#lefttArrow").click(function() {
currentimage = "#image2" counter;
counter ;
$(currentimage).hide();
}
)
});
body {
background-color: lightpink;
}
#header {
text-align: center;
font-size: 30px;
color: yellow;
text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
}
#slideshow {
display: flex;
justify-content: center;
align-items: center;
max-width: 100%;
}
#image1 {
border-radius: 100px;
}
#image2 {
border-radius: 100px;
}
#image3 {
border-radius: 100px;
}
#image1-thumbnail {
border-radius: 120px;
border: 3px solid black;
}
#image2-thumbnail {
border-radius: 120px;
border: 3px solid black;
}
#image3-thumbnail {
border-radius: 120px;
border: 3px solid black;
}
#thumbnails {
display: flex;
justify-content: center;
align-items: center;
max-width: 100%;
gap: 10px;
}
button {
background-color: purple;
font-weight: bold;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="style.css" rel="stylesheet" type="text/css"/>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<title>jQuery</title>
</head>
<body background="candy-background.jpg">
<div id="header">
<h1>Candy</h1>
</div>
<div id="slideshow">
<img id="image1"
src="https://wearenotmartha.com/wp-content/uploads/DIY-Rock-Candy-Featured.jpg" width="400px">
<img id="image2"
src="https://www.speachfamilycandy.com/contents/media/l_smallspicegumdrops.jpg" width="400px">
<img id="image3" src="https://st4.depositphotos.com/4585465/20188/i/1600/depositphotos_201883238-stock-photo-colorful-candies-dessert-background.jpg" width="400px">
</div>
<div id="thumbnails">
<button id="leftArrow"> < </button>
<img id="image1-thumbnail"
src="https://wearenotmartha.com/wp-content/uploads/DIY-Rock-Candy-Featured.jpg" width="100px">
<img id="image2-thumbnail"
src="https://www.speachfamilycandy.com/contents/media/l_smallspicegumdrops.jpg" width="100px">
<img id="image3-thumbnail" src="https://st4.depositphotos.com/4585465/20188/i/1600/depositphotos_201883238-stock-photo-colorful-candies-dessert-background.jpg" width="100px">
<button id="rightArrow"> > </button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
uj5u.com熱心網友回復:
考慮以下示例。
jQuery(function($) {
$("#thumbnails").on('click', "img", function() {
var sel = $(this).index();
$("#slideshow img").fadeOut(1000, function() {
$("#slideshow > img").eq(sel).fadeIn();
});
});
$("#thumbnails").on("click", "button", function() {
var self = $(this);
var current = $("#slideshow > img:visible");
var prev, next;
if (current.index() != 0) {
prev = $("#slideshow > img:last");
} else {
prev = current.prev("img");
}
if (current.index() == $("#slideshow > img").length - 1) {
next = $("#slideshow > img:first");
} else {
next = current.next("img");
}
current.fadeOut(1000, function() {
if (self.attr("id") == "leftArrow") {
prev.fadeIn();
} else {
next.fadeIn();
}
});
});
$("#slideshow > img").hide(function() {
$("#slideshow > img:eq(0)").show();
});
});
body {
background-color: lightpink;
}
#header {
text-align: center;
font-size: 30px;
color: yellow;
text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
}
#slideshow {
display: flex;
justify-content: center;
align-items: center;
width: 400px;
height: 400px;
overflow: hidden;
margin: 0 auto;
}
#image1 {
border-radius: 100px;
}
#image2 {
border-radius: 100px;
}
#image3 {
border-radius: 100px;
}
#image1-thumbnail {
border-radius: 120px;
border: 3px solid black;
}
#image2-thumbnail {
border-radius: 120px;
border: 3px solid black;
}
#image3-thumbnail {
border-radius: 120px;
border: 3px solid black;
}
#thumbnails {
display: flex;
justify-content: center;
align-items: center;
max-width: 100%;
gap: 10px;
}
button {
background-color: purple;
font-weight: bold;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div id="header">
<h1>Candy</h1>
</div>
<div id="slideshow">
<img id="image1" src="https://wearenotmartha.com/wp-content/uploads/DIY-Rock-Candy-Featured.jpg" width="400px">
<img id="image2" src="https://www.speachfamilycandy.com/contents/media/l_smallspicegumdrops.jpg" width="400px">
<img id="image3" src="https://st4.depositphotos.com/4585465/20188/i/1600/depositphotos_201883238-stock-photo-colorful-candies-dessert-background.jpg" width="400px">
</div>
<div id="thumbnails">
<button id="leftArrow"> < </button>
<div>
<img id="image1-thumbnail" src="https://wearenotmartha.com/wp-content/uploads/DIY-Rock-Candy-Featured.jpg" width="100px">
<img id="image2-thumbnail" src="https://www.speachfamilycandy.com/contents/media/l_smallspicegumdrops.jpg" width="100px">
<img id="image3-thumbnail" src="https://st4.depositphotos.com/4585465/20188/i/1600/depositphotos_201883238-stock-photo-colorful-candies-dessert-background.jpg" width="100px">
</div>
<button id="rightArrow"> > </button>
</div>
只有當你有一個大資料集時才需要索引,比如影像路徑陣列。然后更容易擁有索引,以便您可以更好地在陣列中移動。
有了一個只有一個可見的影像元素串列,這本質上就是你的索引。對于左/右箭頭,隱藏當前元素,您可以使用.prev()和.next()顯示下一個元素。對于直接點擊,它們的順序相同,因此很容易獲得.index()縮略圖,隱藏所有其他人,并顯示特定影像。
如果您必須使用計數器,您仍然可以使用相同的代碼執行此操作。
jQuery(function($) {
var counter = 0;
function showImage(index) {
$("#slideshow > img").fadeOut(1000, function() {
console.log("Showing Img " index);
$("#slideshow > img").eq(index).show();
});
}
$("#thumbnails").on("click", "button", function(event) {
var self = $(this);
console.log("Button Click");
if (self.is("#leftArrow")) {
counter--;
if (counter < 0) {
counter = $("#slideshow > img").length - 1;
}
} else {
counter ;
if (counter == $("#slideshow > img").length) {
counter = 0;
}
}
showImage(counter);
return false;
});
$("#thumbnails").on("click", "img", function(event) {
var self = $(this);
console.log("Img Click");
counter = self.index();
showImage(counter);
return false;
});
showImage(counter);
});
body {
background-color: lightpink;
}
#header {
text-align: center;
font-size: 30px;
color: yellow;
text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
}
#slideshow {
display: flex;
justify-content: center;
align-items: center;
width: 400px;
height: 400px;
overflow: hidden;
margin: 0 auto;
}
#image1 {
border-radius: 100px;
}
#image2 {
border-radius: 100px;
}
#image3 {
border-radius: 100px;
}
#image1-thumbnail {
border-radius: 120px;
border: 3px solid black;
}
#image2-thumbnail {
border-radius: 120px;
border: 3px solid black;
}
#image3-thumbnail {
border-radius: 120px;
border: 3px solid black;
}
#thumbnails {
display: flex;
justify-content: center;
align-items: center;
max-width: 100%;
gap: 10px;
}
button {
background-color: purple;
font-weight: bold;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div id="header">
<h1>Candy</h1>
</div>
<div id="slideshow">
<img id="image1" src="https://wearenotmartha.com/wp-content/uploads/DIY-Rock-Candy-Featured.jpg" width="400px">
<img id="image2" src="https://www.speachfamilycandy.com/contents/media/l_smallspicegumdrops.jpg" width="400px">
<img id="image3" src="https://st4.depositphotos.com/4585465/20188/i/1600/depositphotos_201883238-stock-photo-colorful-candies-dessert-background.jpg" width="400px">
</div>
<div id="thumbnails">
<button id="leftArrow"> < </button>
<div>
<img id="image1-thumbnail" src="https://wearenotmartha.com/wp-content/uploads/DIY-Rock-Candy-Featured.jpg" width="100px">
<img id="image2-thumbnail" src="https://www.speachfamilycandy.com/contents/media/l_smallspicegumdrops.jpg" width="100px">
<img id="image3-thumbnail" src="https://st4.depositphotos.com/4585465/20188/i/1600/depositphotos_201883238-stock-photo-colorful-candies-dessert-background.jpg" width="100px">
</div>
<button id="rightArrow"> > </button>
</div>
使用原始代碼。
$(document).ready(function() {
let counter = 1;
$("#image2").hide();
$("#image3").hide();
$("body").on('click', "#image2-thumbnail", function() {
currentimage = "#image" counter;
counter = 2;
$(currentimage).hide();
$("#image1, #image3").slideUp('slow', function() {
$("#image2").slideDown('slow');
});
});
$("body").on('click', "#image3-thumbnail", function() {
currentimage = "#image" counter;
counter = 3;
$(currentimage).hide();
$("#image1, #image2").fadeOut(1000, function() {
$("#image3").fadeIn(1000);
});
});
$("body").on('click', "#image1-thumbnail", function() {
currentimage = "#image" counter;
counter = 1;
$(currentimage).hide();
$("#image2, #image3").fadeOut(1000, function() {
$("#image1").fadeIn(1000);
});
});
$("#rightArrow").click(function() {
currentimage = "#image" counter ;
$(currentimage).hide();
$("#image" counter).show();
})
$("#lefttArrow").click(function() {
currentimage = "#image2" counter--;
$(currentimage).hide();
$("#image" counter).show();
})
});
body {
background-color: lightpink;
}
#header {
text-align: center;
font-size: 30px;
color: yellow;
text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
}
#slideshow {
display: flex;
justify-content: center;
align-items: center;
max-width: 100%;
}
#image1 {
border-radius: 100px;
}
#image2 {
border-radius: 100px;
}
#image3 {
border-radius: 100px;
}
#image1-thumbnail {
border-radius: 120px;
border: 3px solid black;
}
#image2-thumbnail {
border-radius: 120px;
border: 3px solid black;
}
#image3-thumbnail {
border-radius: 120px;
border: 3px solid black;
}
#thumbnails {
display: flex;
justify-content: center;
align-items: center;
max-width: 100%;
gap: 10px;
}
button {
background-color: purple;
font-weight: bold;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div id="header">
<h1>Candy</h1>
</div>
<div id="slideshow">
<img id="image1" src="https://wearenotmartha.com/wp-content/uploads/DIY-Rock-Candy-Featured.jpg" width="400px">
<img id="image2" src="https://www.speachfamilycandy.com/contents/media/l_smallspicegumdrops.jpg" width="400px">
<img id="image3" src="https://st4.depositphotos.com/4585465/20188/i/1600/depositphotos_201883238-stock-photo-colorful-candies-dessert-background.jpg" width="400px">
</div>
<div id="thumbnails">
<button id="leftArrow"> < </button>
<img id="image1-thumbnail" src="https://wearenotmartha.com/wp-content/uploads/DIY-Rock-Candy-Featured.jpg" width="100px">
<img id="image2-thumbnail" src="https://www.speachfamilycandy.com/contents/media/l_smallspicegumdrops.jpg" width="100px">
<img id="image3-thumbnail" src="https://st4.depositphotos.com/4585465/20188/i/1600/depositphotos_201883238-stock-photo-colorful-candies-dessert-background.jpg" width="100px">
<button id="rightArrow"> > </button>
</div>
您代碼中的主要問題是您的選擇器。你有,在很多地方:
currentimage = "#image2" counter;
然后你的選擇器變成了#image21最初的樣子。此選擇器不選擇任何專案。所以我們把它改成:
currentimage = "#image" counter;
這#image1最初給了我們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/403803.html
標籤:
上一篇:將預選值填充到Select2
下一篇:cgb2111-day06
