首页 > 解决方案 > 在媒体查询中创建 jquery 规则,有可能吗?

问题描述

我在 jquery 中创建事件时遇到问题。我的代码是这样的:

在 THML 中没有问题,因为它引用了一个事件。我在 jquery 中附加了代码:

$("nav ul li a").click(function(e) {
    e.preventDefault();
    var target =  $(this).attr("href");
    $("html,body").animate({
        scrollTop: $(target).offset().top
    },2000,"easeOutQuint");
});

这样,当我单击导航条目时,它会将我带到所需的元素......这里没有问题。

然后我创建了一个允许我全屏查看导航的hamb,它安静地打开和关闭,这是代码。

$("#hamb").click(function(e) {
    if( $(this).hasClass("on") ){
        $(this).removeClass("on");
        $("#full").stop().fadeIn(800);
        $("#full ul li").addClass("down");
        $(this).css("z-index", "20");
        $("#hamb span:nth-of-type(1)").addClass("ruota1");
        $("#hamb span:nth-of-type(2)").css("opacity", "0");
        $("#hamb span:nth-of-type(3)").addClass("ruota3");
    } else {
        $(this).addClass("on");
        $("#full").stop().fadeOut(300);
        $("#full ul li").removeClass("down");
        $(this).css("z-index", "10");
        $("#hamb span:nth-of-type(1)").removeClass("ruota1");
        $("#hamb span:nth-of-type(2)").css("opacity", "1");
        $("#hamb span:nth-of-type(3)").removeClass("ruota3");
    }
});

我想确保当我点击一个声音时,导航会关闭。我已经尝试过这样做(#full 是导航):

$("nav ul li a").click(function(e) {
       e.preventDefault();
       var target =  $(this).attr("href");
       $("html,body").animate({scrollTop: $(target).offset().top},2000,"easeOutQuint");
       $("#full").stop().fadeOut(800);
    });

它有效,它悄悄地消失了..问题是导航在经典的水平版本中也消失了..我怎样才能确保这个规则只存在于窗口的某个宽度?所以你只使用平板电脑和手机?我试过:

if ($(window).width() < 960) {
   example....
}

但是我不能很好地使用这个规则..我不能,有人可以帮助我吗?

标签: jqueryhtmlcss

解决方案


推荐阅读