我写了一个函数,在单击箭头时左右移动滑块。滑块的工作原理是更改网址中的哈希值。
功能:
var navigateGalleryForward = function() {
var pathName = window.location.href;
if (pathName.indexOf("item-1") != -1) {
window.location.hash = "item-2";
} else if (pathName.indexOf("item-2") != -1) {
window.location.hash = "item-3";
} else if (pathName.indexOf("item-3") != -1) {
window.location.hash = "item-1";
} else {
console.log("Something went wrong navigating the gallary..")
}
};
var navigateGalleryBackward = function() {
var pathName = window.location.href;
if (pathName.indexOf("item-1") != -1) {
window.location.hash = "item-3";
} else if (pathName.indexOf("item-2") != -1) {
window.location.hash = "item-1";
} else if (pathName.indexOf("item-3") != -1) {
window.location.hash = "item-2";
} else {
console.log("Something went wrong navigating the gallary..")
}
};
$(document).ready(function() {
$("#left-arrow").on('click', function () {
console.log("I work");
navigateGalleryBackward();
});
$("#right-arrow").on('click', function () {
console.log('work');
navigateGalleryForward();
});
});
箭头示例:
<figure class="item one">
<img onClick="navigateGalleryBackward" id="left-arrow" src="{{ 'arrow-left.png' | asset_url }}" alt="left">
<img onclick="navigateGalleryForward" id="right-arrow"src="{{ 'arrow-right.png' | asset_url }}" alt="right">
<h1 style="color: white; font-weight:bold; font-size: 2rem; margin-top:2em; ">5 New Sensors for Control4</h1>
<p style="color: white; font-size:1.2rem;">Learn more about Dome's new line of cross-compatible sensors.</p>
<img style="height: 150px; margin-top: 3em; margin-right: 3em;"class="figure_h" src="{{ 'slide_show_1.jpg' | asset_url }}" />
<button style="margin-top: 3em; display:block; margin: auto;" class="btn_k">BUY NOW</button>
</figure>
所以在图一,它的确有效。但在第二和第三位上它并没有。它被解雇后,它不会再次开火。
图二和三:
<figure class="item two">
<img onClick="navigateGalleryBackward" id="left-arrow" src="{{ 'arrow-left.png' | asset_url}}" alt="left">
<img onclick="navigateGalleryForward" id="right-arrow" src="{{ 'arrow-right.png' | asset_url }}" alt="right">
<h1 class="figure_h">2</h1>
</figure>
<figure class="item three">
<img onClick="navigateGalleryBackward" id="left-arrow" src="{{ 'arrow-left.png' | asset_url }}" alt="left">
<img onclick="navigateGalleryForward" id="right-arrow" src="{{ 'arrow-right.png' | asset_url }}" alt="right">
<h1 class="figure_h">3</h1>
</figure>
我怎样才能使这个工作?
谢谢!
答案 0 :(得分:1)
你的箭头都有相同的ID,试着给它们一个类,然后在你的脚本中使用它。
答案 1 :(得分:0)
HTML元素id对于两个或更多元素不能相同。