我在 jQuery 中有侧边导航操作:
$(function openSidebar()) {
document.getElementById("sidebar").style.width = "250px";
document.getElementById("wrapper").style.marginLeft = "250px";
}
function closeSidebar() {
document.getElementById("sidebar").style.width = "0";
document.getElementById("wrapper").style.marginLeft= "0";
}
我需要在移动设备上将宽度从 250px 更改为 100%。我如何在 jQuery 上获取它?
最佳答案
if ($(window).width() < 768) {
$(function openSidebar()) {
document.getElementById("sidebar").style.width = "100%";
document.getElementById("wrapper").style.marginLeft = "100%";
}
function closeSidebar() {
document.getElementById("sidebar").style.width = "0";
document.getElementById("wrapper").style.marginLeft = "0";
}
} else {
$(function openSidebar()) {
document.getElementById("sidebar").style.width = "250px";
document.getElementById("wrapper").style.marginLeft = "250px";
}
function closeSidebar() {
document.getElementById("sidebar").style.width = "0";
document.getElementById("wrapper").style.marginLeft = "0";
}
}
更新:
if ($(window).width() < 768) {
var width = "100%";
var marginLeft = "100%";
} else {
var width = "250px";
var marginLeft = "250px";
}
function openSidebar() {
$("#sidebar").width(width);
$("#wrapper").css('margin-left', marginLeft );
};
关于javascript - jquery 媒体查询上的 jQuery 侧边导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44206610/