简体   繁体   English

根据滚动位置隐藏和显示导航箭头

[英]Hiding and Showing navigation arrows based on scroll position

I have a horizontal list with arrows that navigate through it, and I'm trying to get the get the right arrow to disappear when it reaches the end (and the same for the left arrow). 我有一个带有在其中导航的箭头的水平列表,并且我试图使右箭头在到达终点时消失(左箭头也是如此)。 It's works.....sort of. 很好.....有点 My issue is that there is an extra click needed to make them disappear, even though the click before it will reach the end of the list. 我的问题是,即使单击之前的单击将到达列表的末尾,也需要额外单击以使其消失。 Any advice would be greatly appreciated! 任何建议将不胜感激!

JsFiddle link JsFiddle链接

//Hide left at start
$('#lefty').hide();
//left arrow controls
$('#lefty').click(function(){
    if($('.container').scrollLeft() == 0)

        {$('#lefty').hide()}
    $('#righty').show();   
 $(".container").animate({scrollLeft: "-=100px"})
 })
//right arrow controls
$('#righty').click(function(){
    if ($('.container')[0].scrollWidth - $('.container').scrollLeft() == $('.container').width())
      {$("#righty").hide()}
    $('#lefty').show(); 
 $(".container").animate({scrollLeft: "+=100px"})
 })

You need to apply the conditional after the animation is finish, in the current code is evaluating the conditional with some value during the animation. 您需要在动画结束后应用条件,在当前代码中,在动画过程中使用一些值来评估条件。 Use this: 用这个:

//Hide left at start
$('#lefty').hide();

//left arrow controls
$('#lefty').click(function(){

    $('#righty').show();   
    $(".container").animate({scrollLeft: "-=100px"}, "normal", function(){
        if($('.container').scrollLeft() <= 0){
            $('#lefty').hide();
        }
    });
});

//right arrow controls
$('#righty').click(function(){
    $('#lefty').show(); 

    $(".container").animate({scrollLeft: "+=100px"}, "normal", function(){
        if ($('.container')[0].scrollWidth - $('.container').scrollLeft() == $('.container').width()){
            $("#righty").hide();
        }
    });
});

I'm not sure if this is the best way but works. 我不确定这是否是最好的方法,但是可以。 I just add a div to print the width and just change the validation. 我只是添加了一个div以打印宽度并更改了验证。 Look the jsfiddle to view how it works. 查看jsfiddle以查看其工作方式。

js fiddle js小提琴

//Hide left at start
$('#lefty').hide();
//left arrow controls
$('#lefty').click(function(){
    $("#mensaje").append("<br>"+$('.container').scrollLeft());
if($('.container').scrollLeft() <= 100 )
    {$('#lefty').hide()}
$('#righty').show();   
$(".container").animate({scrollLeft: "-=100px"})
})
//right arrow controls
$('#righty').click(function(){
        $("mensaje").append("<br>");
        $("#mensaje").append($('.container')[0].scrollWidth -     $('.container').scrollLeft());
if ($('.container')[0].scrollWidth - $('.container').scrollLeft() <= 500)
  {$("#righty").hide()}
$('#lefty').show(); 
$(".container").animate({scrollLeft: "+=100px"})
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM