jquery - 窗口大小的 setInterval + jQuery CSS 动画

标签 jquery css animation setinterval window-resize

我正在努力解决这个问题,我已经尝试了各种方法,虽然从逻辑上讲它应该有效,但它就是无效。任何帮助都会很棒,因为我不太擅长编码。

因此,我正在尝试为一个对象设置从左侧到右侧末端的动画。并且定位因屏幕尺寸而异。

到目前为止,这就是我所拥有的。我正在检测用户的浏览器大小,但我希望在检测到新的调整大小时也销毁 setIntervals。

showViewportSize();
var $box = $('#box');

$(window).resize(function (e) {
    window.clearInterval('animation');
    showViewportSize();
});

function showViewportSize() {
    var width = $(window).width();
    var height = $(window).height();

    if (width <= 1024) {
        var box = setInterval(function () {
            $box.animate({
                "left": "+1200"
            }, 40000, function () {
                $box.removeAttr("style");
            });
        }, 400);
    }

    if ((width <= 1440) && (width > 1025)) {
        var box = setInterval(function () {
            $box.animate({
                "left": "+1200"
            }, 40000, function () {
                $box.removeAttr("style");
            });
        }, 400);
    }

    if (width >= 2000) {
        var box = setInterval(function () {
            $box.animate({
                "left": "+1200"
            }, 40000, function () {
                $box.removeAttr("style");
            });
        }, 400);
    }
}

最佳答案

将 CSS3 转换与良好的“ol 媒体查询”相结合怎么样?

#box {
    left: 0;

    -webkit-transition: left 0.4s ease-in-out;
    -moz-transition: left 0.4s ease-in-out;
    -o-transition: left 0.4s ease-in-out;
    transition: left 0.4s ease-in-out;
}

@media screen and (min-width : 1024px) {
    #box {
        left: 1200px;
    }
}

应该可以,但尚未测试。

关于jquery - 窗口大小的 setInterval + jQuery CSS 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15815289/

相关文章:

缩略图上的 jquery lightbox 单击更改主图像

javascript - 使用 jquery 将 css 应用到所有 id

html - IE9 (ie) 兼容模式中断 "select"

javascript - 使用 jquery mobile 隐藏和显示 div

qt - QML - 需要了解如何制作波浪动画

jQuery - 动画 css 背景大小?

java - 单击任意位置后,将 fillAfter 设置为 true 的 Android 动画会重置

javascript - 动态添加子项时如何设置父项可滚动

Javascript:未定义不是函数错误

css - 在 rails 3 中使用带有 button_to "delete"的 Bootstrap 图标