Javascript 根据URL在两个不同的CSS定义之间切换

Javascript 根据URL在两个不同的CSS定义之间切换,javascript,jquery,css,Javascript,Jquery,Css,我有一个CSS,它控制着网站桌面版本上的一些模态 #modal, #purchModal, #travModal, #notesModal, #explainModal { position: fixed; z-index: 101; top: 10%; left: 25%; width: 50%; background:#CEECF5; border-radius:15px; } 如果url包含“/mobile/”,我想将那里的CSS代码切换到 z-index: 101; to

我有一个CSS,它控制着网站桌面版本上的一些模态

#modal, #purchModal, #travModal, #notesModal, #explainModal 
{ position: fixed; z-index: 101; top: 10%; left: 25%; 
   width: 50%; background:#CEECF5; border-radius:15px; 
}
如果url包含“/mobile/”,我想将那里的CSS代码切换到

 z-index: 101; top: 5%; left: 5%; width: 95%; 
 background:#CEECF5; border-radius:15px; 

到底什么是最好的方法?我想我会在这里使用jQuery或JavaScript

为这两种样式使用两个不同的类,并像它们一样切换

.Class1 { 
    position: fixed; z-index: 101; top: 10%; left: 25%; 
    width: 50%; background:#CEECF5; border-radius:15px; 
}

.Class2 {
    z-index: 101; top: 5%; left: 5%; width: 95%; 
    background:#CEECF5; border-radius:15px; 
}
$('Element you want to change css').toggleClass('Class1' , 'Class2');
然后像这样切换它们

.Class1 { 
    position: fixed; z-index: 101; top: 10%; left: 25%; 
    width: 50%; background:#CEECF5; border-radius:15px; 
}

.Class2 {
    z-index: 101; top: 5%; left: 5%; width: 95%; 
    background:#CEECF5; border-radius:15px; 
}
$('Element you want to change css').toggleClass('Class1' , 'Class2');
或者你也可以像这样直接使用

if(document.url.indexOf('mobile')!=-1)
{
    $('Element you want to change css').addClass('Class1');
}
else
{
    $('Element you want to change css').addClass('Class2');
}

如果需要,您可以删除旧类,然后按照上述方法添加新类。

为样式和切换使用两个不同的类

.Class1 { 
    position: fixed; z-index: 101; top: 10%; left: 25%; 
    width: 50%; background:#CEECF5; border-radius:15px; 
}

.Class2 {
    z-index: 101; top: 5%; left: 5%; width: 95%; 
    background:#CEECF5; border-radius:15px; 
}
$('Element you want to change css').toggleClass('Class1' , 'Class2');
然后像这样切换它们

.Class1 { 
    position: fixed; z-index: 101; top: 10%; left: 25%; 
    width: 50%; background:#CEECF5; border-radius:15px; 
}

.Class2 {
    z-index: 101; top: 5%; left: 5%; width: 95%; 
    background:#CEECF5; border-radius:15px; 
}
$('Element you want to change css').toggleClass('Class1' , 'Class2');
或者你也可以像这样直接使用

if(document.url.indexOf('mobile')!=-1)
{
    $('Element you want to change css').addClass('Class1');
}
else
{
    $('Element you want to change css').addClass('Class2');
}

如果需要,可以删除旧类,然后按照上述方法添加新类。

首先,定义两种CSS变体:

/* Desktop */
#modal, #purchModal, #travModal, #notesModal, #explainModal {
  position: fixed; z-index: 101; top: 10%; left: 25%; 
  width: 50%; background:#CEECF5; border-radius:15px; 
}

/* Mobile */
body.mobile #modal, body.mobile #purchModal, body.mobile #travModal,
body.mobile #notesModal, body.mobile #explainModal {
  z-index: 101; top: 5%; left: 5%; width: 95%; 
  background:#CEECF5; border-radius:15px;
}
然后,让jQuery根据页面加载时的URL设置
类:

$(function() {
  var isMobile = /\/mobile\/.test(window.location.url);
  $('body').toggleClass('mobile', isMobile);
});

首先,定义两种CSS变体:

/* Desktop */
#modal, #purchModal, #travModal, #notesModal, #explainModal {
  position: fixed; z-index: 101; top: 10%; left: 25%; 
  width: 50%; background:#CEECF5; border-radius:15px; 
}

/* Mobile */
body.mobile #modal, body.mobile #purchModal, body.mobile #travModal,
body.mobile #notesModal, body.mobile #explainModal {
  z-index: 101; top: 5%; left: 5%; width: 95%; 
  background:#CEECF5; border-radius:15px;
}
然后,让jQuery根据页面加载时的URL设置
类:

$(function() {
  var isMobile = /\/mobile\/.test(window.location.url);
  $('body').toggleClass('mobile', isMobile);
});

有两个不同的css文件怎么样?一个用于桌面版,另一个用于移动版?在页面中更改css文件,如下所示

$('head').append('<link rel="stylesheet" href="desktop.css" type="text/css" />');
$('head')。追加('');

两个不同的css文件如何?一个用于桌面版,另一个用于移动版?在页面中更改css文件,如下所示

$('head').append('<link rel="stylesheet" href="desktop.css" type="text/css" />');
$('head')。追加('');

无需使用jQuery或JavaScript即可完成。 只需为不同的设备编写css

//A comman css for class #modal
 #modal {
   position: fixed;
   top: 10%;
   left: 50%;
   z-index: 1050;
   width: 560px;
   margin-left: -280px;
   background-color: #ffffff;
 }

//if screen width is less then 767px
@media (max-width: 767px) {
 #modal {
    position: fixed;
    top: 20px;
    right: 20px;
    left: 20px;
    width: auto;
    margin: 0;
  }
}

//if screen width is less then 480px
@media (max-width: 480px) {
 #modal {
    top: 10px;
    right: 10px;
    left: 10px;
  }
}

因此,您可以在不包含这个不需要的JS jquery的情况下缩短加载时间。

您可以在不使用jquery或JavaScript的情况下完成这项工作。 只需为不同的设备编写css

//A comman css for class #modal
 #modal {
   position: fixed;
   top: 10%;
   left: 50%;
   z-index: 1050;
   width: 560px;
   margin-left: -280px;
   background-color: #ffffff;
 }

//if screen width is less then 767px
@media (max-width: 767px) {
 #modal {
    position: fixed;
    top: 20px;
    right: 20px;
    left: 20px;
    width: auto;
    margin: 0;
  }
}

//if screen width is less then 480px
@media (max-width: 480px) {
 #modal {
    top: 10px;
    right: 10px;
    left: 10px;
  }
}
因此,您可以在不包含这个不需要的JS jquery的情况下提高加载时间