Javascript 移动设备上的响应侧边栏禁用

Javascript 移动设备上的响应侧边栏禁用,javascript,jquery,html,css,jquery-mobile,Javascript,Jquery,Html,Css,Jquery Mobile,我使用下面的代码显示一个响应侧栏,当用户滚动到顶部和/或单击菜单链接div时,该侧栏在登录时可见。如果用户已展开侧边栏,则当用户向上或向下滚动100px时,侧边栏将再次折叠 这在桌面上非常有效,但是在移动设备上并不是最好的用户体验。我想禁用脚本中在移动设备上自动展开/折叠的部分,即更改您的功能 if ($window.width() < 768) {} if($window.width()414){}你不能给你的JS添加一个简单的 if($window.width()>414){ 如果

我使用下面的代码显示一个响应侧栏,当用户滚动到顶部和/或单击菜单链接div时,该侧栏在登录时可见。如果用户已展开侧边栏,则当用户向上或向下滚动100px时,侧边栏将再次折叠

这在桌面上非常有效,但是在移动设备上并不是最好的用户体验。我想禁用脚本中在移动设备上自动展开/折叠的部分,即更改您的功能

if ($window.width() < 768) {}
if($window.width()<768){}

如果($window.width()>414){}你不能给你的JS添加一个简单的

if($window.width()>414){
如果(相对值>100 | |相对值<-100){
$(“#侧栏”).removeClass(“活动”);
$(“#sidebarToggle”).removeClass(“活动”);
$(“.line”).css(“顶部”、“0”)
}else if($window.width()>768){
$(“#侧栏”).addClass(“活动”);
$(“#sidebarToggle”).addClass(“活动”);
}

}
实际上,我更喜欢
$window.width()
。谢谢你的建议,不过我需要保留滚动功能。我似乎找到了问题所在,并更新了问题。请参阅“更新”。你能解释一下吗?如果你想让
else
子句只在768px以上运行,你可以把它变成
else if($window.width()>768px)
。我更新了答案。我希望这有帮助。只需使用
width()
jquery方法计算窗口视口的宽度!旁注。。不要忘记缓存jquery对象<代码>$(“#侧边栏”)
$(“#侧边栏”)
应该在滚动处理程序的外部定义,以防止每次事件发生时DOM拥挤(在下面使用sizzle引擎)。否则,处理程序可能需要更多的时间来执行(特别是在一段时间内多次触发的事件,如滚动、mousemove等),有时会导致滞后效果。我不知道如何实施你的建议,但我真的很感谢你抽出时间。你能帮忙吗?我似乎找到了问题所在,并更新了问题。请参阅“更新”。你能解释一下吗?谢谢你的建议!我似乎找到了问题所在,并更新了问题。请参阅“更新”。你能解释一下吗?
/* Expand/Collapse sidebar based on position in page */
var relativeY = $("#sidebar").offset().top - $(".line").offset().top;

$(window).scroll(function() {

  relativeY = $("#sidebar").offset().top - $(".line").offset().top;
  console.log(relativeY);
  if (relativeY > 100 || relativeY < -100) {
    $("#sidebar").removeClass("active");
    $("#sidebarToggle").removeClass("active");
    $(".line").css("top", "0")
  } else {
    $("#sidebar").addClass("active");
    $("#sidebarToggle").addClass("active");
  }
});

// Add or remove 'active' class upon landing/resize based on viewport size
(function($) {
  var $window = $(window),
    $sidebar = $('#sidebar');
  $sidebarToggle = $('#sidebarToggle');

  function resize() {
    if ($window.width() <= 768) {
      return $sidebar.removeClass('active');
      return $sidebarToggle.removeClass('active');
    }

    $sidebar.addClass('active');
    $sidebarToggle.addClass('active');
  }

  $window
    .resize(resize)
    .trigger('resize');
})(jQuery);

/* Responsive sidebar */
(function(window, document) {

  var sidebar = document.getElementById('sidebar'),
    sidebarToggle = document.getElementById('sidebarToggle');

  function toggleClass(element, className) {
    var classes = element.className.split(/\s+/),
      length = classes.length,
      i = 0;

    for (; i < length; i++) {
      if (classes[i] === className) {
        classes.splice(i, 1);
        break;
      }
    }
    // The className is not found
    if (length === classes.length) {
      classes.push(className);
    }

    element.className = classes.join(' ');
  }

  sidebarToggle.onclick = function(e) {
    var topScroll = $(window).scrollTop();
    var active = 'active';
    $(".line").css("top", topScroll);
    e.preventDefault();
    toggleClass(sidebar, active);
    toggleClass(sidebarToggle, active);
  };

}(this, this.document));
body {
  height: 4000px;
}


/* Sidebar */

.line {
  /* Invisible line from which to measure position of #sidebar for expand/collapse purposes */
  top: 0;
  position: absolute;
}

#sidebar {
  background: #212e37;
  color: #8b8c91;
  width: 225px;
  height: 100%;
  position: fixed;
  top: 0;
  bottom: 0;
  right: 35px;
  margin-right: -225px;
  /* "#sidebar" width */
  z-index: 9998;
  -webkit-overflow-scrolling: touch;
  box-shadow: none;
  -webkit-transition: all 0.7s ease-in-out;
  -moz-transition: all 0.7s ease-in-out;
  -ms-transition: all 0.7s ease-in-out;
  -o-transition: all 0.7s ease-in-out;
  transition: all 0.7s ease-in-out;
}

#sidebar.active {
  margin-right: 0;
  box-shadow: -10px 0 30px rgba(0, 0, 0, 0.35);
}

@media only screen and (min-width: 0px) and (max-width: 414px) {
  #sidebar {
    overflow-y: scroll;
    width: 75%;
    margin-right: -75%;
  }
}

#sidebar header {
  font-weight: bold;
  padding: 30px 40px 50px 20px;
  border-bottom: 1px solid #8b8c91;
  color: #8b8c91;
}

@media only screen and (min-width: 0px) and (max-width: 414px) {
  #sidebar header {
    padding: 20px;
  }
}


/* Toggle menu bar */

.menu-link {
  position: fixed;
  display: block;
  top: 0;
  right: 0;
  background: #7e775d;
  z-index: 9999;
  width: 35px;
  height: 100%;
  padding: 35px 11px;
}

.menu-link:hover {
  background: #211d28;
}
    else  {
        $("#sidebar").addClass("active");
        $("#sidebarToggle").addClass("active");
    }
if ($window.width() < 768) {}