Javascript $(窗口).resize();没有按预期工作

Javascript $(窗口).resize();没有按预期工作,javascript,jquery,Javascript,Jquery,我想在一组功能上使用大于768px的屏幕尺寸,另一组用于小于768px的任何东西。我需要.resize() 如果刷新页面,则.resize()事件内部的代码可以正常工作,但就好像没有触发事件一样。有什么想法吗 <script> jQuery(document).ready(function($){ function mobileFilterMenu(){ var screenTest = $(window).width(); if (scree

我想在一组功能上使用大于768px的屏幕尺寸,另一组用于小于768px的任何东西。我需要
.resize()

如果刷新页面,则
.resize()
事件内部的代码可以正常工作,但就好像没有触发事件一样。有什么想法吗

<script>
jQuery(document).ready(function($){
    function mobileFilterMenu(){
        var screenTest = $(window).width();
        if (screenTest >= 769){
            $(window).on('scroll', function () {
                    var scrollTop     = $(window).scrollTop(),
                    elementOffset = $('.inventory-search > .col-md-9').offset().top,
                    distance      = (elementOffset - scrollTop);
                if(distance <= 0){
                    $('.filter-form').css('top', Math.abs(distance));
                }else{
                    $('.filter-form').css('top', 0);
                }
            });
        }else{
            var vHeight = $(window).height() - $('#switch').height() - $('#primary-menu-toggle').height();
            $('.filter-form').css('height', vHeight + 'px');
            vTextHeight = vHeight - 20;
            $('.wpv-filter-form').css('height', vTextHeight + 'px');
            objNegHeight = (vHeight * -1) + 50;
            $('.filter-form').css('top', objNegHeight + 'px');

            var i;
            $('#switch').click(function(){
                if (i === 0){
                    $('.filter-form').css('top', objNegHeight + 'px');
                    i++;
                }else{
                    $('.filter-form').css('top', '50px');
                    i = 0;
                }
            });
        }
    }
    mobileFilterMenu();
    $(window).resize(mobileFilterMenu());
});
</script>

jQuery(文档).ready(函数($){
函数mobileFilterMenu(){
var screenstest=$(window.width();
如果(屏幕测试>=769){
$(窗口).on('scroll',函数(){
var scrollTop=$(窗口).scrollTop(),
elementOffset=$('.inventory search>.col-md-9').offset().top,
距离=(elementOffset-scrollTop);

如果(距离您应该使用
媒体查询
根据视口尺寸而不是代码应用
CSS

媒体查询使我们能够创建响应性体验,其中 特定样式适用于小屏幕、大屏幕和屏幕 媒体查询语法允许创建 可根据设备特性应用的规则

虽然我们可以查询几个不同的项目,但是 最常见的响应性网页设计有最小宽度、最大宽度、, 最小高度和最大高度


@介质(最小宽度:500px)和(最大宽度:600px){
h1{
颜色:紫红色;
}
.描述:之后{
内容:“事实上,它的宽度在500像素到600像素之间。”;
}
}

只需更改这部分代码。这将解决您的问题

$(window).resize(mobileFilterMenu);
不要传递mobileFilterMenu(),而是传递此mobileFilterMenu

$window.resize中的参数应具有函数而不是函数调用

希望这能解决您的问题。

您做错了!!
mobileFilterMenu()
调用该函数。您想传递它。
$(窗口)。在('resize',mobileFilterMenu);
<link rel="stylesheet" media="(max-width: 640px)" href="max-640px.css">
<link rel="stylesheet" media="(min-width: 640px)" href="min-640px.css">
<link rel="stylesheet" media="(orientation: portrait)" href="portrait.css">
<link rel="stylesheet" media="(orientation: landscape)" href="landscape.css">
<style>
  @media (min-width: 500px) and (max-width: 600px) {
    h1 {
      color: fuchsia;
    }

    .desc:after {
      content:" In fact, it's between 500px and 600px wide.";
    }
  }
</style>
$(window).resize(mobileFilterMenu);