javascript - jquery 媒体查询上的 jQuery 侧边导航

标签 javascript jquery html css twitter-bootstrap

我在 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/

相关文章:

javascript - jQuery Mobile popup data-dismissible ="false"问题

Javascript定位树节点

javascript - 在 jQuery 中使用过滤器

jquery - 使嵌套的子元素全宽浏览器

javascript - Vuejs 使用方法发送 GET 请求

javascript - 移动网络 - 隐藏的数字输入

javascript - jQuery 如何将我的有序列表转换为数组?

javascript - Jquery 拖放 - 在其他容器之前插入拖动的 div 容器

javascript - 在网络应用程序上禁用 Chrome 移动设备的 "tap to search"

javascript - 从服务器检索 JSON.parse() 字符串