javascript - jquery 计算高度和宽度并在其背后设置动画内容

标签 javascript html css animation

  • h1
  • 背后的动画内容
  • 如果 h1 位于图像的顶部,则不要设置动画

目前工作正常,但我还需要计算 h1 标签的宽度,以便为 h1 之外的图像设置动画。 基本上,如果图像位于 h1 的宽度之外,则应该显示。

我希望这已经够清楚了。 Demo.

js:

// Get the divs that should change
function displayThese() {
    var $heading = $('h1');
    var h1top = $heading.position().top;
    var h1bottom = h1top + $heading.height();

    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() {
    var divs = displayThese();
    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
    });
});

最佳答案

你只需要使用 $.fn.width :

var $heading = $('h1');
var h1top = $heading.position().top;
var h1bottom = h1top + $heading.height();
var h1left = $heading.position().left;
var h1right = h1top + $heading.width();

var divs = $('li').filter(function () {
    var $e = $(this);
    var top = $e.position().top;
    var bottom = top + $e.height();
    var left = $e.position().left;
    var right = left + $e.width();

    return top > h1bottom || bottom < h1top || left > h1right || right < h1left;
});
return divs;

这是一个 fiddle :http://jsfiddle.net/33Ec8/3/

关于javascript - jquery 计算高度和宽度并在其背后设置动画内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24410173/

相关文章:

javascript - ngRoute 导致 AngularJS 中的黑屏?

javascript - 表单进度条

javascript - 平滑滚动到页面上的 div ID 通过 URL 地址加载

javascript - 基于 JavaScript 的跨平台移动应用程序分析

javascript - 用单引号捕获在三引号字符串中

javascript - 有没有办法通过 Jquery 访问 Css 样式

html - 页眉和页脚之间 100% 的内容

javascript - querySelectorAll 和 getElementsBy* 方法返回什么?

javascript - 重写 JavaScript 动画以防止浏览器中缩放/模糊

html - 我应该从 UTF-8 更改为 UTF-16 以适应我的 HTML 中的中文字符吗?