简体   繁体   English

如果坐在另一个元素下面,则不应发生jQuery随机动画

[英]Jquery random animation shouldn't happen if sitting underneath another element

Firstly sorry for the complicity of my question! 首先,对我的问题的同谋感到抱歉!

I have a random animation that makes all the images displaying with different opacity. 我有一个随机动画,可以使所有图像以不同的不透明度显示。 Some of them are sitting underneath an h1 tag. 其中一些坐在h1标签下面。 So all the images underneath the h1 tag shouldn't animate. 因此,h1标签下的所有图像都不应设置动画。

Demo: http://jsfiddle.net/G25fT/ 演示: http//jsfiddle.net/G25fT/

Currently I got tonly the random function working: 目前,我可以使随机函数起作用:

(function fadeInDiv() {
    var divs = $('li');
    var elem = divs.eq(Math.floor(Math.random() * divs.length));
    if (!elem.is(':visible')) {
        elem.prev().remove();
        elem.animate({
            opacity: 1
        }, Math.floor(Math.random() * 1000), fadeInDiv);
    } else {

        elem.animate({
            opacity: (Math.random() * 1)
        }, Math.floor(Math.random() * 1000), function () {
            window.setTimeout(fadeInDiv);
        });
    }

})();

Try using a filter to reduce the selectable items, based on the relative vertical positions to the h1 (the .display div has zero height). 尝试使用filter根据h1的相对垂直位置( .display div的高度为零)减少可选项目。 I check to see whether the items overlap the title at all and exclude them: 我检查一下这些项目是否与标题完全重叠,并排除它们:

eg 例如

var divs = $('li').filter(function () {
        var $e = $(this);
        var top = $e.position().top;
        var bottom = top + $e.height();
        return top > h1bottom || bottom < h1top;
    });

JSFiddle: http://jsfiddle.net/TrueBlueAussie/G25fT/5/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/G25fT/5/

(function fadeInDiv() {
    // exclude any divs 
    var $heading = $('.heading h1');
    var h1top = $heading.position().top;
    var h1bottom = h1top + $heading.height();
    //console.log("top="+ h1top + " bottom = " + h1bottom);
    var divs = $('li').filter(function () {
        var $e = $(this);
        var top = $e.position().top;
        var bottom = top + $e.height();
        return top > h1bottom || bottom < h1top;
    });
    //console.log("Len=" + divs.length);
    var elem = divs.eq(Math.floor(Math.random() * divs.length));
    if (!elem.is(':visible')) {
        elem.prev().remove();
        elem.animate({
            opacity: 1
        }, Math.floor(Math.random() * 1000), fadeInDiv);
    } else {

        elem.animate({
            opacity: (Math.random() * 1)
        }, Math.floor(Math.random() * 1000), function () {
            window.setTimeout(fadeInDiv);
        });
    }

})();

Update: 更新:

If you want to remove the styling on resize (when items move out from under the heading) 如果要删除调整大小时的样式(当项目从标题下方移出时)

JSFiddle: http://jsfiddle.net/TrueBlueAussie/G25fT/11/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/G25fT/11/

// Get the divs that should change
function displayThese() {
    var $heading = $('.heading h1');
    var h1top = $heading.position().top;
    var h1bottom = h1top + $heading.height();
    //console.log("top="+ h1top + " bottom = " + h1bottom);
    var divs = $('li').filter(function () {
        var $e = $(this);
        var top = $e.position().top;
        var bottom = top + $e.height();
        return top > h1bottom || bottom < h1top;
    });
    return divs;
}

(function fadeInDiv() {
    // exclude any divs 
    var divs = displayThese();
    //console.log("Len=" + divs.length);
    var elem = divs.eq(Math.floor(Math.random() * divs.length));
    if (!elem.is(':visible')) {
        elem.prev().remove();
        elem.animate({
            opacity: 1
        }, Math.floor(Math.random() * 1000), fadeInDiv);
    } else {

        elem.animate({
            opacity: (Math.random() * 1)
        }, Math.floor(Math.random() * 1000), function () {
            window.setTimeout(fadeInDiv);
        });
    }

})();

$(window).resize(function () {
    // Get items that do not change    
    var divs = $('li').not(displayThese());
    divs.css({
        opacity: .3
    });
});

A simple way to detect if the image is under the title would be something like: 一种检测图像是否在标题下的简单方法是:

var titleTop = $('.heading').position().top;
var divs = $('li');
var elem = divs.eq(Math.floor(Math.random() * divs.length));
var elemTop = elem.position().top;
var elemHeight = elem.height();


if (titleTop < elemTop || titleTop > elemTop + elemHeight) {
    //it's not overlapping, do the animation
} else {
    //it's overlapping
}

Basically it's saying "if the title's position is less than the position of the current element or if the title's position is more than the the current element position + the current element's height, do the animation, otherwise don't" 基本上是说“如果标题的位置小于当前元素的位置,或者标题的位置大于当前元素的位置+当前元素的高度,请进行动画处理,否则请不要”

I did it here: http://jsfiddle.net/jonigiuro/G25fT/2/ You'll need to rework your loop though, but it should get you going 我在这里做过: http : //jsfiddle.net/jonigiuro/G25fT/2/虽然您需要重做循环,但是它应该可以使您前进

If your page has a scroll and you see it stops working, try using offset() instead of postition() 如果您的页面有滚动条,并且您看到它停止工作,请尝试使用offset()而不是postition()

可能的解决方案:您可以找出标题的水平和垂直偏移,然后将其始终与图像的偏移进行比较

With the current html you'll need to determine how many li are in the first row (we'll call it numFirstRow) and change you div selector to this: 使用当前的html,您需要确定第一行中有多少li(我们将其称为numFirstRow),然后将div选择器更改为:

var divs = $('li:nth-child(n+numFirstRow)');

implemented it here: http://jsfiddle.net/G25fT/7/ 在这里实现它: http : //jsfiddle.net/G25fT/7/

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

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