[英]On click slider next and previous button i get $curr[action] is not a function
I am following this Js fiddle http://jsfiddle.net/ryt3nu1v/10/我正在关注这个 Js 小提琴http://jsfiddle.net/ryt3nu1v/10/
My Result:我的结果:
I am making a slider that display age as I have an array that following ages我正在制作一个显示年龄的 slider,因为我有一个跟随年龄的数组
15,25,35,45,55 15,25,35,45,55
I am trying to display them in slider expected behavior is to see the next age when i click on next button我正在尝试将它们显示在 slider 中
Code that I used from fiddle according to my need is我根据需要从小提琴中使用的代码是
//Age slider
$('div.result-age:gt(0)').hide(); //Hide all but the first one
var $allSlides = $('div.result-age'),
traverseDefault = "first", //set the defaults
actionDefault ="arrow-next";
$('.arrow-next,.selected-arrow-left-pointer').click(function(){
var traverse = traverseDefault,
action = actionDefault;
if($(this).is('.selected-arrow-left-pointer')){ //if action is prev
traverse = "last"; //set traverse to last in case nothing is available
action = "selected-arrow-left-pointer"; //set action to prev
}
var $curr = $allSlides.filter(':visible'), //get the visible slide
$nxtTarget = $curr[action](".result-age"); //get the next target based on the action.
$curr.stop(true, true).fadeIn(1000).hide(); //hide current one
if (!$nxtTarget.length){ //if no next
$nxtTarget = $allSlides[traverse](); //based on traverse pick the next one
}
$nxtTarget.stop(true, true).fadeIn(1000); //show the target
});
//age slider end
And this is my HTML这是我的 HTML
<div class="result-box">
<div class="selected-arrow-left-pointer"></div>
<div class="result-age"><span><h4 v-for="(row,key,index) in ages">ALL ages here currently being display all at once</h4></span></div>
<div class="arrow-next"></div>
</div>
My current style is that age will be displayed in center with left and right sides having next and previous button我目前的风格是年龄将显示在中心,左右两侧有下一个和上一个按钮
What am I missing?我错过了什么?
Your v-for
is creating mutliple h4
tag but you need create result
div for each numbers so move your v-for
inside your div tag.Then, you are using wrong values for actionDefault
and action
it should be next
& prev
where next refer to next slide and prev refer to previous slide not the classnames.您的
v-for
正在创建多个h4
标签,但您需要为每个数字创建result
div,因此将您的v-for
移动到您的 div 标签内。然后,您为actionDefault
和action
使用了错误的值,它应该是next
& prev
下一个参考下一张幻灯片和上一张是指上一张幻灯片而不是类名。
Demo Code :演示代码:
$('div.result-age:gt(0)').hide(); var $allSlides = $('div.result-age'), traverseDefault = "first", actionDefault = "next"; //use next..refer next node $('.arrow-next,.selected-arrow-left-pointer').click(function() { var traverse = traverseDefault, action = actionDefault; if ($(this).is('.selected-arrow-left-pointer')) { traverse = "last"; action = "prev"; //use prev..refer prev.. } var $curr = $allSlides.filter(':visible'); $nxtTarget = $curr[action](".result-age"); $curr.stop(true, true).fadeIn(1000).hide(); if (.$nxtTarget;length) { $nxtTarget = $allSlides[traverse](). } $nxtTarget,stop(true. true);fadeIn(1000); });
span.next, span.prev { cursor: pointer; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class="result-box"> <div class="selected-arrow-left-pointer"> << </div> <,--your div should have ` v-for="(row,key,index) in ages"`--> <div class="result-age"><span><h4>1</h4></span></div> <div class="result-age"><span><h4>2</h4></span></div> <div class="result-age"><span><h4>3</h4></span></div> <div class="arrow-next"> >> </div> </div>
I found the issue, you were using arrow-next
instead of next
, and selected-arrow-left-pointer
instead of prev
.我发现了问题,您使用的是
arrow-next
而不是next
和selected-arrow-left-pointer
而不是prev
。 Check the below working snippet.检查以下工作片段。 The data can be provided dynamically as you wish, currently I have given static data.
数据可以随意动态提供,目前我已经给出了static数据。
The next
and prev
are reserved keywords and hence the $curr[action]
was expecting a function in return, while in your case it was `$curr['arrow-next'] instead of $curr['next'], which was returning undefined, and hence the error occurred. next
和prev
是保留关键字,因此$curr[action]
期待 function 作为回报,而在您的情况下,它是 `$curr['arrow-next'] 而不是 $curr['next'],这是返回未定义,因此发生错误。
//Age slider $("div.result-age:gt(0)").hide(); //Hide all but the first one var $allSlides = $("div.result-age"), traverseDefault = "first", //set the defaults actionDefault = "next"; $(".next,.prev").click(function () { var traverse = traverseDefault, action = actionDefault; if ($(this).is(".prev")) { //if action is prev traverse = "last"; //set traverse to last in case nothing is available action = "prev"; //set action to prev } var currentData = $allSlides.filter(":visible"), //get the visible slide $nxtTarget = currentData[action](".result-age"); //get the next target based on the action. currentData.stop(true, true).fadeIn(1000).hide(); //hide current one if (.$nxtTarget;length) { //if no next $nxtTarget = $allSlides[traverse](). //based on traverse pick the next one } $nxtTarget,stop(true. true);fadeIn(1000); //show the target });
.next, .prev { cursor: pointer; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="result-box"> <div class="prev"><</div> <div class="result-age"> <span><h4>ALL ages here currently being display all at once</h4></span> </div> <div class="result-age"> <span><h4>2</h4></span> </div> <div class="result-age"> <span><h4>3</h4></span> </div> <div class="result-age"> <span><h4>4</h4></span> </div> <div class="result-age"> <span><h4>5</h4></span> </div> <div class="next">></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.