填充,边距和背景色不会消失

时间:2018-07-02 06:50:59

标签: jquery html css background padding

背景

我正在尝试使用其中一个选项的下拉菜单制作固定的导航栏。在较小的屏幕上,菜单覆盖会显示在屏幕中间垂直显示的所有选项。我不希望小屏幕版本中的任何链接具有背景色/悬停。导航背景应仅为#fff#111,具体取决于您所使用的主题(在.mobile-menu和nav中将“浅色”更改为“深色”以查看其他菜单主题) 。屏幕宽度只是测试的占位符。我在JavaScript中遗留了一些旧版本(例如navList),对此很抱歉–请忽略该内容。

问题

  • 子链接上的背景色和悬停背景色不会消失。
  • 具有子链接的链接太高了,我似乎无法摆脱其填充/边距。

我尝试过的

我尝试使用覆盖

* { background-color: transparent }

* { padding: 0; margin: 0 }
在小屏幕媒体查询底部的

无济于事,以及在单个元素和/或元素类上重置这些属性。

我还尝试对链接2(<a>)使用<div>而不是.dropbtn,但这导致.dropdown-content出现在链接2的正上方而不是从下面开始。我可以通过top: 47px上的.dropdown-content之类的问题来解决此问题,但实际上更喜欢使用<div>如何使菜单正确显示在链接2下(并且找不到该参数会导致<a>链接2的行为类似于<div>)。

禁用所有JavaScript也无法修复。

代码

这是到目前为止我得到的: https://jsfiddle.net/heyycap/2p0zq61e/1355/

var mobile = $('.mobile-menu')
var navList = $('nav > ul')
var winHeight
var docHeight
var throttleScroll
var scrollTop
var nav = $('nav')
var light = $('.light')
var dark = $('.dark')
var breakPoint = 600
var windowSize = 0
var newWindowSize = 0
var lightColor = '#fff'
var darkColor = '#111'

var scrollDiv = document.createElement("div");
scrollDiv.className = "scrollbar-measure";
document.body.appendChild(scrollDiv);
var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
document.body.removeChild(scrollDiv);
console.info('Scrollbar: ' + scrollbarWidth + 'px')

function styleLightDark() {
  light.css({
    'background-color': lightColor,
    'color': darkColor
  })
  dark.css({
    'background-color': darkColor,
    'color': lightColor
  })
}

function styleMobileMenu() {
  if (mobile.hasClass('light')) {
    mobile.css('background-color', lightColor)
  } else {
    mobile.css('background-color', darkColor)
  }
}

function styleNav() {
  if ($('nav').hasClass('light')) {
    $('nav').css('background-color', lightColor)
  } else {
    $('nav').css('background-color', darkColor)
  }
}

function remBg() {
  light.css('background-color', '')
  dark.css('background-color', '')
}

function changeNav(x) {
  if (windowSize == 'small') {
    if (x <= breakPoint) {
      nav.hide()
    }
  }
  if (windowSize == 'large') {
    if (x > breakPoint) {
      nav.fadeIn(500)
    }
  }
}

function checkWindowSize(x) {
  if (x <= breakPoint) {
    newWindowSize = 'small'
  }
  if (x > breakPoint) {
    newWindowSize = 'large'
  }
}

function getMeasurements() {
  var winHeight = $(window).height()
  var docHeight = $(document).height()
  var trackLength = docHeight - winHeight
  var innerWidth = $(document).width()
  var widthWithScrollbar = innerWidth + scrollbarWidth
  amountScrolled()
  console.info('Width - scrollbar, if it exists: ' + innerWidth + 'px')
  if (winHeight == docHeight) {
    checkWindowSize(innerWidth)
    if (windowSize != newWindowSize) {
      windowSize = newWindowSize
      changeNav(innerWidth)
    }
  } else {
    checkWindowSize(widthWithScrollbar)
    if (windowSize != newWindowSize) {
      windowSize = newWindowSize
      changeNav(widthWithScrollbar)
    }
  }
}

function amountScrolled() {
  scrollTop = $(window).scrollTop()
  console.info('Scrolled: ' + scrollTop + 'px')
  if ((scrollTop < 5) && (innerWidth > breakPoint)) {
    remBg()
  } else if ((scrollTop < 5) && (nav.is(':hidden')) && (innerWidth <= breakPoint)) {
    remBg()
  } else if ((!nav.is(':hidden')) && (innerWidth <= breakPoint)) {
    mobile.css('background-color', '')
  } else if ((scrollTop >= 5) && (nav.is(':hidden')) && (innerWidth <= breakPoint)) {
    remBg()
    styleMobileMenu()
  } else if ((scrollTop >= 5) && (innerWidth > breakPoint)) {
    styleLightDark()
  }
}

getMeasurements()

$(window).on('resize', function() {
  getMeasurements()
})

$(window).on('scroll', function() {
  clearTimeout(throttleScroll);
  throttleScroll = setTimeout(function() {
    amountScrolled()
  }, 10)
})

mobile.on('click', function() {
  scrollTop = $(window).scrollTop()
  if (nav.is(':hidden')) {
    nav.fadeIn(500)
    remBg()
    styleNav()
  } else if ((!nav.is(':hidden')) && (scrollTop > 5)) {
    nav.fadeOut(500)
    remBg()
    styleMobileMenu()
  } else {
    nav.fadeOut(500)
    remBg()
  }
})
body {
  background-color: lightblue;
  margin: 50px 0;
}

.mobile-menu {
  display: none;
  transition: .5s ease;
}

nav {
  position: fixed;
  width: 100%;
  top: 0;
  opacity: .9;
  transition: .5s ease;
}

nav a {
  float: left;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  padding: 14px 16px;
}

nav.dark a:hover,
nav.dark .dropdown:hover .dropbtn {
  background-color: #fff;
  color: #111;
}

nav.light a:hover,
nav.light .dropdown:hover .dropbtn {
  background-color: #111;
  color: #fff;
}

.dropdown-content {
  display: none;
  position: absolute;
  z-index: 1;
}

.dropdown-content a {
  float: none;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown:hover .dropdown-content {
  display: block;
}

#navigation,
#resources,
.resources {
  display: none;
}

.light * {
  color: #111;
}

.dark * {
  color: #fff;
}

.dark .dropdown-content {
  background-color: #111;
}

.light .dropdown-content {
  background-color: #fff;
}

.scrollbar-measure {
  width: 100px;
  height: 100px;
  overflow: scroll;
  position: absolute;
  top: -9999px;
}


/* MEDIA QUERIES */

@media (max-width: 600px) {
  .mobile-menu {
    display: block;
    position: fixed;
    text-align: right;
    width: 100%;
    top: 0;
    right: 0;
    cursor: pointer;
    z-index: 3;
    height: auto;
    opacity: .9;
    align-items: center;
    padding: 14px 16px;
    overflow: hidden;
  }
  nav {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    display: none;
  }
  nav>div {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
  }
  nav a,
  .dropdown,
  #navigation,
  #resources,
  .resources,
  .dropdown-content a {
    float: none;
    display: block;
    text-align: center;
    position: relative;
    padding: 1px 0;
  }
  .dropdown-content,
  .dropbtn {
    float: none;
    display: block;
    text-align: center;
    position: relative;
    padding: 0;
  }
  #navigation:after {
    content: "Navigation";
  }
  #resources:after {
    content: "Resources";
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <a class="mobile-menu light">menu</a>
  <nav class="light">
    <div>
      <div id="navigation"></div>
      <a href="#">Link 1</a>
      <div class="dropdown">
        <div href="#" class="dropbtn">Link 2</div>
        <div class="dropdown-content">
          <a href="#">Link 2-1</a>
          <a href="#">Link 2-2</a>
          <a href="#">Link 2-3</a>
          <a href="#">Link 2-4</a>
          <a href="#">Link 2-5</a>
        </div>
      </div>
      <a href="#">Link 3</a>
      <a href="#">Link 4</a>
      <a href="#">Link 5</a>
      <a href="#">Link 6</a>
      <a href="#">Link 7</a>
      <div id="resources"></div>
      <div class="resources">
        <a href="#">Link 8</a>
        <a href="#">Link 9</a>
        <a href="#">Link 10</a>
        <a href="#">Link 11</a>
      </div>
    </div>
  </nav>
  <section>
    <p>Test text</p>
    <p>some text to wrap : some text to wrap : some text to cause scrolling : : some text to wrap : some text to wrap : some text to cause scrolling : : some text to wrap : some text to wrap : some text to cause scrolling : : some text to wrap : some text
      to wrap : some text to cause scrolling : : some text to wrap : some text to wrap : some text to cause scrolling : : some text to wrap : some text to wrap : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>some text to cause scrolling : </p>
    <p>Last bit of text</p>
  </section>
</body>

1 个答案:

答案 0 :(得分:0)

使用媒体查询添加这些样式

@media screen and (max-width: 767px) {
  nav.light a:hover, nav.light .dropdown:hover .dropbtn {
      background-color: transparent;
      color: black;
  }
  .dropdown .dropbtn {
    padding: 0 16px;
  }
}

Updated fiddle