简体   繁体   English

使用jQuery单击按钮后,如何跳到特定幻灯片?

[英]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.

相关问题 单击“提交”按钮后如何在网页上显示文本 - How do I display text on a webpage upon clicking a 'Submit' button 如何在单击按钮时转换 animation? - How do I transition an animation upon clicking a button? 通过单击猫头鹰轮播上的链接跳转到特定的幻灯片 - Jump to specific slide by by clicking link on owl carousel 如何在页面加载时使用javascript / jquery滑动/打开特定菜单项? - How do I slide/open specific menu item using javascript / jquery on page load? 当有人使用jQuery单击子导航项时,如何在Flexslider中显示特定幻灯片? - How do i show a specific slide in Flexslider when someone clicked on sub nav item using jQuery? 单击取消静音按钮后如何将音量设置为特定值? - How to set volume to be at a specific value upon clicking unmute button? 使用 jquery 单击删除按钮后更改行背景颜色 - Change Row background color upon clicking delete button using jquery 如何仅通过单击按钮浏览图片(如幻灯片)? - How do I go through pictures (like a slide show) just by clicking a button? 如何通过单击Javascript中的按钮跳到另一页? - How can I jump to another page by clicking a button in Javascript? 单击同一行中另一个单元格中的图像后,如何替换特定单元格上的单词? - How do I replace a word on a specific cell upon clicking on an image in another cell that is on the same row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM