Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bootstrap 4 如何在Bootstrap 4导航栏中将大型设备上的粘性顶部位置更改为小型设备上的固定顶部位置?_Bootstrap 4 - Fatal编程技术网

Bootstrap 4 如何在Bootstrap 4导航栏中将大型设备上的粘性顶部位置更改为小型设备上的固定顶部位置?

Bootstrap 4 如何在Bootstrap 4导航栏中将大型设备上的粘性顶部位置更改为小型设备上的固定顶部位置?,bootstrap-4,Bootstrap 4,你能告诉我有没有办法把导航条的位置从大型设备上的粘性顶部改为小型设备上的固定顶部 这可以用javascript完成。 首先,从navbar元素中删除sticky top类 所以,它应该是这样的: <nav id="menu" class="navbar navbar-expand-md navbar-dark bg-dark"> 然后将该脚本添加到head标记中,以便该脚本加载Domcontent var width = window.screen.width; var n

你能告诉我有没有办法把导航条的位置从大型设备上的粘性顶部改为小型设备上的固定顶部



这可以用javascript完成。
首先,从navbar元素中删除sticky top类

所以,它应该是这样的:

<nav id="menu" class="navbar navbar-expand-md navbar-dark bg-dark">

然后将该脚本添加到head标记中,以便该脚本加载Domcontent

var width = window.screen.width;
var nav = document.getElementById('menu');
if(width < 768){
    if(nav.hasClass('sticky-top')){
        nav.classList.remove("sticky-top");
    }
    nav.classList.add("fixed-top");
}
else{
    if(nav.hasClass('fixed-top')){
        nav.classList.remove("fixed-top");
    }
    nav.classList.add('sticky-top');
}
var-width=window.screen.width;
var nav=document.getElementById('menu');
如果(宽度<768){
if(nav.hasClass('sticky-top')){
导航类列表。移除(“粘性顶部”);
}
导航类列表添加(“固定顶部”);
}
否则{
if(导航类(“固定顶部”)){
导航类列表。移除(“固定顶部”);
}
nav.classList.add('sticky-top');
}

我希望它能帮助您

在小型设备中使用CSS媒体查询

将此添加到样式表中,它应该可以完成此任务。确保将样式表添加到bootstrap.css文件下面

// Small devices
@media (max-width: 767px) { 

  #menu sticky-top {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    z-index: 1030;
  }
}
// Small devices
@media (max-width: 767px) { 

  #menu sticky-top {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    z-index: 1030;
  }
}