【问题标题】:when each element not visible, change its position当每个元素不可见时,更改其位置
【发布时间】:2014-08-26 16:36:53
【问题描述】:

我正在使用 $.each 来查找我网页上的每个“屏幕”。我希望在每个屏幕变得可见时发生一些事情,然后当元素不再可见时该动作自行反转。这是最后一部分不起作用。

我正在使用 jquery 可见插件。

jquery:

$( window ).scroll(function() { //check for visibles    
$.each($(".picture"), function(){
                var $this = $(this);
                if ($(this).visible(true)) {

                    setTimeout(function()
                    {
                        $this.children('.cutline_background').animate({"left":"0px"},2500, function(){});
                        $this.children('.text').animate({"left":"40px"},2500, function(){});
                    }, 3000);

                } else{
                    $this.children('.cutline_background').css("left", "-20%");  
                    $this.children('.text').css("left", "-20%");                                                                              }
            });
});

可见部分有效。但是,在不可见的部分,我做错了什么?它似乎不再认出我的“这个”了。

【问题讨论】:

  • 你为什么要重新声明var $this = $(this);?另外根据我的理解$(this).visible(true) 应该是$this.is(':visible')
  • @Satpal:我正在向下滚动页面。当某物进入视野时,它是可见的。与css无关。
  • 而且@Satpal 评论也与 CSS 无关。这是关于在 if 内部重新声明 $this 的无用的,以及在此 if 中检查元素的可见性时出错。
  • @Regent,我正在使用可见的 jquery 插件。 github.com/teamdf/jquery-visible

标签: jquery css jquery-plugins


【解决方案1】:

问题在于延迟显示使元素的隐藏变得混乱。例如,滚动到元素,设置显示超时,然后滚动离开此元素,应用隐藏,然后启动显示动画。

我建议保持对象的状态(例如,使用data-state)并在显示和隐藏之前检查它以处理所有这些情况。

如何做到这一点的例子在updated fiddle。

我没有解决的唯一小问题是当您滚动离开元素并在超时期间滚动回它时重置超时。

完整代码:

var isHidden = 0;
var isAnimated = 1;
var isShown = 2;
var shouldBeHidden = 3;

$(document).ready(function()
{
    $(".picture").data('state', isHidden);

    $(window).scroll(function()
    {
        $(".picture").each(function()
        {
            var $this = $(this);
            var thisIsVisible = $this.visible(true);
            var thisState = $this.data('state');
            if (thisIsVisible && thisState == isHidden)
            {
                $this.data('state', isAnimated);            
                setTimeout
                (
                    function()
                    {
                        showElement($this);
                    },
                    3000
                );
            }
            else if (thisIsVisible && thisState == shouldBeHidden)
            {
                $this.data('state', isAnimated); 
            }
            else if (!thisIsVisible && thisState == isShown)
            {
                hideElement($this);
            }
            else if (!thisIsVisible && thisState == isAnimated)
            {
                $this.data('state', shouldBeHidden); 
            }
        }); 
    });
});

function showElement($this)
{
    if ($this.data('state') == shouldBeHidden)
    {
        $this.data('state', isHidden);
        return;
    }
    $this.children('.cutline_background').animate
    (
        {
            "left": "0px"
        },
        2500
    );
    $this.children('.text').animate
    (
        {
            "left": "40px"
        },
        2500,
        function()
        {
            if ($this.data('state') == shouldBeHidden)
            {
                hideElement($this);
            }
            else
            {
                $this.data('state', isShown);
            }
        }
    );
}

function hideElement($this)
{
    $this.children('.cutline_background').css("left", "-20%");  
    $this.children('.text').css("left", "-20%");
    $this.data('state', isHidden);
}

【讨论】:

  • 谢谢,Regent,但是这段代码和我现在的代码一样——元素滑出很好,但它永远不会重置。
  • @LauraNMS 你的意思是.cutline_background 仍然显示在元素上,即使你滚动离开这个元素并且变得不可见?如果是这样 - 确定你真的滚动离开元素并且它真的不可见吗?
  • 是的,但是请看我上面的回答。
  • @LauraNMS 我已经看到了你的答案,但是延迟显示也有同样的问题。而且我仍然不知道我的小提琴出了什么问题,因为它对我很有效。
【解决方案2】:

我决定使用航点插件而不是可见插件,这是可行的:

$(".picture").waypoint(function(direction){
        var $this = $(this);
        if (direction === "down") {
            setTimeout(function()
                {
                    $this.children('.cutline_background').animate({"left":"0px"},2500, function(){});
                    $this.children('.text').animate({"left":"40px"},2500, function(){});
                }, 3000);

        }
        if (direction === "up") { 
            $this.children('.cutline_background').css("left", "-20%");  
            $this.children('.text').css("left", "-20%");

        }
    });

【讨论】:

  • 经过更多的实验,我完全放弃了这个想法。这太不完美了。现在,当您到达页面时,我已经展开了元素。当您单击“x”时,它会缩回并显示“i”图标。当您单击“i”时,元素会展开。
猜你喜欢
  • 2016-03-15
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-08
相关资源
最近更新 更多