[英]Having trouble implementing “jump to slide” in a jQuery slider
I've been trying this for the past 2 days and can't seem to figure out a solution.. 过去两天我一直在尝试这个,似乎无法找到解决方案..
I have the prev and next arrows working for navigation (along with arrow keys). 我有上一个和下一个箭头用于导航(以及箭头键)。 But now that I have a timeline, I can't seem to 'jump to' slides using a div-id (correctly)?
但是现在我有一个时间轴,我似乎无法使用div-id(正确)“跳转到”幻灯片?
So for example, if I wanted to go from section 1 to section 5, I want to be able to click on my section 5 button and it jumps to that slide. 例如,如果我想从第1部分到第5部分,我希望能够点击我的第5部分按钮并跳转到该幻灯片。
Here is my working example . 这是我的工作范例 。 The slider's timeline appears on slide 2+.
幻灯片的时间轴显示在幻灯片2+上。 For the example, I am only working within the target section.
例如,我只在目标部分工作。
This is the code I am using to "jump to a slide": 这是我用来“跳转到幻灯片”的代码:
$('.slideshow-timeline a').click(function() {
var target_id = $(this).attr('href');
removeClasses();
$($(".tour-panel")[current]).addClass("fadeOutRight");
setTimeout(function() {
$(target_id).addClass("active-tour fadeInLeft");
}, 50);
setTimeout(function() {
$($(".tour-panel")[current]).removeClass("fadeOutRight");
}, 750);
current = target_id.split('-')[1] || 0;
});
But for some reason, I get 2 specific problems: 但出于某种原因,我遇到了两个具体问题:
Problem 1: I click to go to a slide, then use arrow keys to go back ~ this causes me to jump back 2 slides.
问题1:我点击进入幻灯片,然后使用箭头键返回〜这使我跳回2张幻灯片。
Problem 2: I click to go to a slide, then use arrow keys to go forward ~ this breaks my slider and shows a white screen
问题2:我点击进入幻灯片,然后使用箭头键前进〜这会打破我的滑块并显示白色屏幕
I believe the majority of the probelem lies within this line of code: 我相信大多数探针都在这行代码中:
current = target_id.split('-')[1] || 0;
But I am not 100% sure and need your help, I have prepared a very basic fiddle example here . 但我不是100%肯定,并需要你们的帮助,我已经准备了一个非常基本的小提琴例子在这里 。
Some things I've tried was messing with the split()
variables, swapping out nextElement();
我尝试过的一些事情是搞乱
split()
变量,交换nextElement();
and previousElement();
和
previousElement();
and I just can not find a solution that works. 我找不到有效的解决方案。
All help is very appreciated! 非常感谢所有帮助!
Check out that fiddle: 看看小提琴:
https://jsfiddle.net/gjyswrr9/19/ https://jsfiddle.net/gjyswrr9/19/
Problems were in 3 places: 问题分为三个方面:
var previous = parseInt(current, 10) - 1;
var next = parseInt(current) + 1;
(they were strings, not ints). (它们是字符串,而不是整数)。
And 和
current = target_id.split('-')[1] - 1 || 0;
(you count links starting with 1) (你计算以1开头的链接)
Hope it works as you need. 希望它能按你的需要工作。
Here you go -> fiddle 你去吧 - > 小提琴
What I've basicly changed is your current
value, which you are defining in your click function. 我基本上改变的是您
current
值,您在点击功能中定义了该值。 Actually, your id
value is a string, so firing nextElement()
won't change the value, instead it will concatenate it. 实际上,你的
id
值是一个字符串,所以触发nextElement()
不会改变值,而是会连接它。 This results in some weird behaviour, so instead of changing from target 0
to 1
, you just change from 0
to 01
. 这导致了一些奇怪的行为,所以不是从目标更改
0
到1
,你只需要改变从0
到01
。
Also I've adjusted your $('.slideshow-timeline a').click(function() { }
to behave the same way as your nextElement()
function does. 此外,我已经调整了你的
$('.slideshow-timeline a').click(function() { }
的行为方式与nextElement()
函数相同。
Edit: I forgot to mention 编辑:我忘了提
I believe the majority of the probelem lies within this line of code:
我相信大多数探针都在这行代码中:
current = target_id.split('-')[1] || 0;
You've been right with this. 你是对的。 As mentioned above,
target_id.split('-')[1]
will return you a string value, not a number and this again, as mentioned above, leads to the weird behaivour where your code just concatenates your returned string with a number. 如上所述,
target_id.split('-')[1]
将返回一个字符串值,而不是一个数字,如上所述,这将导致奇怪的行为,您的代码只是将您返回的字符串与数字连接起来。 To achieve the goal you want, you have to parse target_id.split('-')[1]
's value into a number
value by using JavaScripts built-in parseInt()
function. 为了达到你想要的目的,你必须分析
target_id.split('-')[1]
的值转换成number
通过使用JavaScript的内置值parseInt()
函数。
The result is the following: 结果如下:
current = parseInt(target_id.split('-')[1]) || 0;
Hi Can you please check the below link.. 您好,请查看以下链接..
Is this what you are looking for right? 这是你想要的吗?
Please refer this link [jsFiddle][1] 请参考此链接[jsFiddle] [1]
[1]: https://jsfiddle.net/gjyswrr9/39/
Script code as below, 脚本代码如下,
$(document).ready(function() {
var current = 0;
function previousIndex() {
var previous = parseInt(current,10) - 1;
if(previous == 0) {
previous = $(".tour-panel").size();
}
return previous;
}
function nextIndex() {
var next = parseInt(current,10) + 1;
if(next == $(".tour-panel").size()+1) {
next = 1;
}
return next;
}
function removeClasses() {
$(".tour-panel").each(function(index) {
if(index != current) {
$(this).removeClass("active-tour fadeInRight fadeInLeft fadeOutRight fadeOutLeft");
}
})
}
function nextElement() {
removeClasses();
current = nextIndex();
setTimeout(function() {
$("#target-"+current).addClass("active-tour fadeInRight");
}, 50);
}
function previousElement() {
removeClasses();
current = previousIndex();
setTimeout(function() {
$("#target-"+current).addClass("active-tour fadeInLeft");
}, 50);
setTimeout(function() {
$($(".tour-panel")[nextIndex()]).removeClass("active-tour fadeOutRight");
}, 750);
}
$('.slideshow-timeline a').click(function() {
var target_id = $(this).attr('href');
removeClasses();
$($(".tour-panel")[current]).addClass("fadeOutRight");
setTimeout(function() {
$(target_id).addClass("active-tour fadeInLeft");
}, 50);
setTimeout(function() {
$($(".tour-panel")[current]).removeClass("fadeOutRight");
}, 750);
current = target_id.split('-')[1] || 0;
});
Mousetrap.bind('left', previousElement);
Mousetrap.bind('right', nextElement);
$(".previous").click(previousElement);
$(".next").click(nextElement);
});
You were right... the problem was in this line: 你是对的......问题在于这一行:
current = target_id.split('-')[1] || 0;
try this: 试试这个:
current = (target_id.split('-')[1] - 1) || 0; // cater for zero-index
UPDATE UPDATE
Cleared up some of the issues with this fiddle . 清除了这个小提琴的一些问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.