首页 > 解决方案 > 当屏幕尺寸超过一定宽度尺寸时如何运行javascript?

问题描述

我只想在窗口宽度超过 1024 时运行某些 javascript。脚本很长。我试图将它们与

if ($(window).width() >= 1024

但它仍然无法正常工作。

有人能帮我吗?

这是我想在宽度超过 1024 时运行的脚本:

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
    $(window).load(function() {
        $(window).on("scroll resize", function() {
            var pos = $('#fixPlace').offset();
            $('.post').each(function() {
                if (pos.top >= $(this).offset().top && pos.top <= $(this).next().offset().top) {
                    var newDescr = $(this).find('.feature__description').html();
                    var oldDescr = $("#fixPlace").html();

                    $('#fixPlace').html(newDescr);
                    if (newDescr !== oldDescr) {
                        $("#fixPlace").css('opacity', 0.4).animate({ 'opacity': '1', }, 200);
                        return;
                    }
                }
            });
        });

        $(document).ready(function() {
            $(window).trigger('scroll'); 
        });
    });


    let fixPlace = '#fixPlace';
    let scrollStart = null;
    let scrollEnd = null;
    let floatBoxTop = 0.25 * $(window).height();


    $(document).ready(function() {
        scrollStart = $('#fixPlace').offset().top;
        scrollEnd = $('.transition').offset().top - 680;
    })


    $(window).scroll(scroll)

    function scroll() {
        let scrollTop = $(document).scrollTop() + floatBoxTop;
        scrollTop = Math.min(scrollEnd, Math.max(scrollTop, scrollStart))
        if (scrollTop === scrollStart || scrollEnd === scrollTop) {
            $(fixPlace).css({
                position: 'absolute',
                top: scrollTop
            });
        } else {
            $(fixPlace).css({
                position: 'fixed',
                top: floatBoxTop
            });
        }
    }

标签: javascriptjquerywindowsizewidth

解决方案


推荐阅读