简体   繁体   English

根据屏幕分辨率使用 jquery 添加 css 类名并删除 css 类名

[英]add css classname and remove css classname using jquery based on the screen resolution

I am trying add classname and remove classname based on the resolution using jquery.我正在尝试使用 jquery 根据分辨率添加类名和删除类名。

Here is my Jquery code.这是我的 Jquery 代码。 In mobile mode I am getting totopDesk in desktop mode I am getting totop_tab only in tablet mode I am getting correct class.在移动模式下,我在桌面模式下获得totopDesk我仅在平板电脑模式下获得totop_tab我得到正确的 class。 What I am doing wrong here我在这里做错了什么

function isMobile(){
    if($(window).width() <= 575){
        return true;
   } else {
       return false;
   }
 }
function isTab(){
   if($(window).width() > 575 && $(window).width() <= 992){
      return true;
    } else {
      return false;
    }
  }
 function isDesktop(){
    if($(window).width() >= 992){
        return true;
        } else {
        return false;
      }
  }

$(window).resize(function ()
  {
     if (isMobile()) {
         goMobile()
    }
   else if (isTab()) {
       goTablet()
     } 
    else {
       goDesktop();
     } 
  })

    function goDesktop()
{
    $("#go_btn").addClass("totopDesk");
    $("#go_btn").removeClass("totop_tab");  
    $("#go_btn").removeClass("totop_mob");
}
function goMobile() {
    $("#go_btn").addClass("totop_mob");
    $("#go_btn").removeClass("totopDesk");
    $("#go_btn").removeClass("totop_tab");
}
function goTablet() {
    $("#go_btn").addClass("totop_tab");
    $("#go_btn").removeClass("totop_mob");
    $("#go_btn").removeClass("totop");
    $("#go_btn").removeClass("totopDesk");  
}

without Jquery simple using @Media query I am getting this issue没有 Jquery 简单使用@Media 查询我遇到了这个问题

When I am in Tablet mode in the inspect element 48em class got strike out it showing only deskop css当我在检查元素 48em 中处于平板电脑模式时,class 被删除,它只显示桌面 css

在此处输入图像描述

It appears you have the media query before the desktop CSS.看来您在桌面 CSS 之前有媒体查询。 Since your styles cascade, whatever styles come last are the ones that get applied.由于您的 styles 级联,无论 styles 最后是被应用的。 Therefore, you must move your media query to the bottom of the CSS file but above media queries that are smaller.因此,您必须将媒体查询移至 CSS 文件的底部,但位于较小的媒体查询上方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM