[英]How do I jump to a specific slide upon clicking a button using jQuery?
I am using adopted code that uses switches and hashes to navigate between divs on a website, showing only one at a time, like a slideshow. 我正在使用采用的代码,该代码使用开关和哈希在网站上的div之间导航,一次只显示一个,例如幻灯片。 Here's what it looks like:
看起来是这样的:
$("#forward-button").click(function() {
var h = window.location.hash;
var s = h.replace("#","");
var n = parseInt(s) + 1 + '';
if( s == 20 ) { } else {
$( "#slide-" + s ).toggle( "drop", { direction: "left" }, 400 );
$( "#slide-" + s ).promise().done(function(){
$( "#slide-" + n ).toggle( "drop", { direction: "right" }, 400 ); });
rewriteSlideLabel(n);
window.location.hash = "#" + n;
}
});
This is working great, but I would like the added functionality of having buttons that can jump to specific slides instantly (rather than having to go through one by one to get to where you want). 这很好用,但是我想要具有按钮的附加功能,这些按钮可以立即跳转到特定的幻灯片(而不是一个接一个地转到所需的位置)。
I placed some buttons on the side to accomplish this, but I can't figure out the actual code. 我在侧面放置了一些按钮来完成此操作,但是我无法弄清楚实际的代码。 I've tried things like:
我已经尝试过类似的事情:
$("#button2").click(function() {
jumpToHash("2");rewriteSlideLabel("2");
});
but that doesn't have the fancy animation that makes navigating between slides look professional. 但这没有花哨的动画,使在幻灯片之间导航显得专业。
Please help. 请帮忙。 Thank you!
谢谢!
http://codepen.io/anon/pen/qbNOYG http://codepen.io/anon/pen/qbNOYG
fiddling around with your code, I made a new function for you to use: 摆弄代码,我做了一个新函数供您使用:
var jumpToSlide = function(number) {
var h = window.location.hash;
var s = h.replace("#","");
var n = number;
if( s == 20 ) { } else {
$( "#slide-" + s ).toggle( "drop", { direction: "left" }, 400 );
$( "#slide-" + s ).promise().done(function(){
$( "#slide-" + n ).toggle( "drop", { direction: "right" }, 400 ); });
rewriteSlideLabel(n);
window.location.hash = "#" + n;
}
}
then from there, you can just call the following: 然后从那里可以调用以下命令:
$("#button1").click(function() {
jumpToSlide(1);
});
I hope this helps! 我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.