javascript - 在滚动函数 jQuery 上添加 CSS 属性

标签 javascript html jquery css

我正在尝试创建滚动功能,一旦用户滚动到某个点,菜单就会添加出现的 CSS 背景颜色属性。

但是我遇到了麻烦,我相信我已经使用了正确的滚动函数用法,语法也正确,但是 CSS() jQuery 函数没有像你一样执行操作参见此codepen

当用户滚动到屏幕上的某个点时,如何更改 CSS 属性?

$(document).ready(function() {
  $(window).scroll(function() {
    var scroll = $(window).scrollTop();
    if (scroll >= 100) {
      $(".top-nav").css('background', 'blue');
    } else {
      $(".top-nav").css('background', 'transparent');
    }
  });
});
html,
body {
  box-sizing: border-box;
  font-family: Arial, Helvetica, sans-serif;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: scroll;
  overflow-x: hidden;
}

.main {
  height: 2000px;
}

img {
  width: 100%;
}

body {
  background-color: #f7f7f7;
  transition: margin-right 0.5s, margin-left 0.5s;
}

a {
  text-decoration: none;
  color: white;
}

.wrapper {
  display: grid;
  transition: margin-right 0.5s, margin-left 0.5s;
}

.logo-red {
  color: #005aa3;
  font-size: 37px;
}


/* Top Nav Bar */

.top-nav {
  position: fixed;
  top: 0;
  z-index: 10;
  width: 100%;
  display: grid;
  grid-template-columns: auto auto;
  color: white;
}

.top-nav .nav-scroller {
  background-color: blue;
}

.top-nav h1 {
  padding-left: 3rem;
  font-size: 1.6rem;
}

.top-nav div {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  padding-right: 4rem;
}

.top-nav ul {
  list-style: none;
  display: none;
}

.top-nav li {
  display: inline-block;
  padding: 0.7rem 1rem;
}

.top-nav div ul li a {
  color: #fff;
  text-transform: uppercase;
  text-decoration: none;
  letter-spacing: 0.15em;
  display: inline-block;
  padding: 10px 10px;
  position: relative;
}

.top-nav div ul li a:hover,
.top-nav div ul li a:focus {
  outline: none;
  color: #fff;
  transition: 0.5s all ease;
}

.top-nav div ul li a:after {
  background: none repeat scroll 0 0 transparent;
  bottom: 0;
  content: "";
  display: block;
  height: 2px;
  left: 50%;
  position: absolute;
  background: #0068bd;
  transition: width 0.3s ease 0s, left 0.3s ease 0s;
  width: 0;
}

.top-nav div ul li a:hover:after {
  width: 100%;
  left: 0;
}


/* Burger menu */

.side-nav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 2;
  right: 0;
  background-color: #36454f;
  opacity: 1;
  overflow-x: hidden;
  padding: 60px 0;
  transition: width 0.5s;
  -webkit-box-shadow: inset 12px 0px 18px 0px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: inset 12px 0px 18px 0px rgba(0, 0, 0, 0.75);
  box-shadow: inset 12px 0px 18px 0px rgba(0, 0, 0, 0.75);
}

.side-nav a {
  padding: 10px 10px 10px 30px;
  text-decoration: none;
  text-align: right;
  font-size: 5vw;
  margin: 0 20px;
  color: white;
  border-bottom: 2px #ccc solid;
  display: block;
  transition: 0.3s ease-in-out;
}

.side-nav a:hover {
  color: #fff;
  border-radius: 0.5rem;
}

.side-nav .btn-close {
  position: absolute;
  top: 0;
  left: 0;
  font-size: 50px;
  font-weight: bold;
  border: none;
  margin-left: 0;
}

@media (max-width:1024px) {
  .top-nav {
    min-height: 4rem;
  }
  .top-nav div {
    padding-right: 1rem;
  }
  .top-nav h1 {
    padding-left: 1rem;
    padding-top: 0.6rem;
  }
}

@media (min-width:1024px) {
  #burger-menu {
    display: none;
  }
  .top-nav ul {
    display: inline;
    margin: 0;
    padding: 0;
  }
  .contact form {
    width: 50vw;
  }
  .top-nav h1 {
    padding-top: 0.3rem;
  }
}

#yd {
  font-family: 'Rubik', sans-serif;
  font-style: italic;
  color: #bdbdbd;
}

#yd {
  transition: 0.5s all ease;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Rubik:wght@500&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cairo:wght@600&display=swap" rel="stylesheet">
<script src="https://use.fontawesome.com/releases/v5.0.10/js/all.js"></script>
<div id="grid-wrapper" class="wrapper">
  <nav class="top-nav">
    <h1>
      <a href="#" id="logo">
        <span id="yd">yotam dahan</span>
        <span class="logo-red">.</span>
        <span id="com">COM</span>
      </a>
    </h1>
    <div>
      <ul style="direction: rtl;">
        <li><a href="#our-designs">שירותים</a></li>
        <li><a href="#about-us">תיק עבודות</a></li>
        <li><a href="#contact-form">שמור על קשר</a></li>
      </ul>
      <a href="#!" id="burger-menu" onclick="toggleSideMenu()">
        <i class="fas fa-bars" style="color: white; font-size: 22px;"></i>
      </a>
    </div>
  </nav>
  <div id="side-menu" class="side-nav">
    <a href="#!" class="btn-close" onclick="closeSideMenu()">&times;</a>
    <a href="#our-designs" onclick="closeSideMenu()">שירותים</a>
    <a href="#about-us" onclick="closeSideMenu()">תיק עבודות</a>
    <a href="#contact-form" onclick="closeSideMenu()">שמור על קשר</a>
  </div>
</div>
<div class="main"></div>

最佳答案

问题是因为您在 body 元素上设置了 overflow:scroll,因此 window 不会滚动, >body 是。因此,事件处理程序位于错误的元素上。

另请注意,您应该避免将 CSS 样式(以及 HTML)放入 JS 代码中。更好的方法是使用 CSS 类。然后您可以使用toggleClass()。试试这个:

jQuery($ => {
  $('body').scroll(function () {
    var scroll = $(this).scrollTop();
    $('.top-nav').toggleClass('blue', scroll >= 100);
  });
});
html,
body {
  box-sizing: border-box;
  font-family: Arial, Helvetica, sans-serif;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: scroll;
  overflow-x: hidden;
}
.main {
  height: 2000px;
}

img {
  width: 100%;
}

body {
  background-color: #f7f7f7;
  transition: margin-right 0.5s, margin-left 0.5s;
}
a {
  text-decoration: none;
  color: white;
}

.wrapper {
  display: grid;
  transition: margin-right 0.5s, margin-left 0.5s;
}

.logo-red {
  color: #005aa3;
  font-size: 37px;
}

/* Top Nav Bar */
.top-nav {
  position: fixed;
  top: 0;
  z-index: 10;
  width: 100%;
  display: grid;
  grid-template-columns: auto auto;
  color: white;
}
.top-nav.blue {
  background-color: blue;
}
.top-nav .nav-scroller {
  background-color: blue;
}
.top-nav h1 {
  padding-left: 3rem;
  font-size: 1.6rem;
}

.top-nav div {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  padding-right: 4rem;
}

.top-nav ul {
  list-style: none;
  display: none;
}

.top-nav li {
  display: inline-block;
  padding: 0.7rem 1rem;
}
.top-nav div ul li a {
  color: #fff;
  text-transform: uppercase;
  text-decoration: none;
  letter-spacing: 0.15em;
  display: inline-block;
  padding: 10px 10px;
  position: relative;
}

.top-nav div ul li a:hover,
.top-nav div ul li a:focus {
  outline: none;
  color: #fff;
  transition: 0.5s all ease;
}
.top-nav div ul li a:after {
  background: none repeat scroll 0 0 transparent;
  bottom: 0;
  content: "";
  display: block;
  height: 2px;
  left: 50%;
  position: absolute;
  background: #0068bd;
  transition: width 0.3s ease 0s, left 0.3s ease 0s;
  width: 0;
}
.top-nav div ul li a:hover:after {
  width: 100%;
  left: 0;
}
/* Burger menu */

.side-nav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 2;
  right: 0;
  background-color: #36454f;
  opacity: 1;
  overflow-x: hidden;
  padding: 60px 0;
  transition: width 0.5s;
  -webkit-box-shadow: inset 12px 0px 18px 0px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: inset 12px 0px 18px 0px rgba(0, 0, 0, 0.75);
  box-shadow: inset 12px 0px 18px 0px rgba(0, 0, 0, 0.75);
}

.side-nav a {
  padding: 10px 10px 10px 30px;
  text-decoration: none;
  text-align: right;
  font-size: 5vw;
  margin: 0 20px;
  color: white;
  border-bottom: 2px #ccc solid;
  display: block;
  transition: 0.3s ease-in-out;
}

.side-nav a:hover {
  color: #fff;
  border-radius: 0.5rem;
}

.side-nav .btn-close {
  position: absolute;
  top: 0;
  left: 0;
  font-size: 50px;
  font-weight: bold;
  border: none;
  margin-left: 0;
}
@media (max-width: 1024px) {
  .top-nav {
    min-height: 4rem;
  }
  .top-nav div {
    padding-right: 1rem;
  }
  .top-nav h1 {
    padding-left: 1rem;
    padding-top: 0.6rem;
  }
}

@media (min-width: 1024px) {
  #burger-menu {
    display: none;
  }

  .top-nav ul {
    display: inline;
    margin: 0;
    padding: 0;
  }

  .contact form {
    width: 50vw;
  }
  .top-nav h1 {
    padding-top: 0.3rem;
  }
}
#yd {
  font-family: "Rubik", sans-serif;
  font-style: italic;
  color: #bdbdbd;
}
#yd {
  transition: 0.5s all ease;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Rubik:wght@500&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cairo:wght@600&display=swap" rel="stylesheet">
<script src="https://use.fontawesome.com/releases/v5.0.10/js/all.js"></script>
<div id="grid-wrapper" class="wrapper">
  <nav class="top-nav">
    <h1><a href="#" id="logo"><span id="yd">yotam dahan</span><span class="logo-red">.</span><span id="com">COM</span></a></h1>
    <div>
      <ul style="direction: rtl;">
        <li><a href="#our-designs">שירותים</a></li>
        <li><a href="#about-us">תיק עבודות</a></li>
        <li><a href="#contact-form">שמור על קשר</a></li>
      </ul>
      <a href="#!" id="burger-menu" onclick="toggleSideMenu()"><i class="fas fa-bars" style="color: white; font-size: 22px;"></i></a>
    </div>
  </nav>

  <div id="side-menu" class="side-nav">
    <a href="#!" class="btn-close" onclick="closeSideMenu()">&times;</a>
    <a href="#our-designs" onclick="closeSideMenu()">שירותים</a>
    <a href="#about-us" onclick="closeSideMenu()">תיק עבודות</a>
    <a href="#contact-form" onclick="closeSideMenu()">שמור על קשר</a>
  </div>
</div>
<div class="main"></div>

关于javascript - 在滚动函数 jQuery 上添加 CSS 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61394790/

相关文章:

javascript - 将 if 语句转换为函数的最佳方法是什么?

javascript - 动态div的点击事件不起作用

html - 如何使用 CSS 平滑地上下移动图像?

java - 仅在文件下载完成后设置 cookie。

javascript - 2个函数,2个ajax请求,如何正确回调

javascript - 如何允许弹出窗口继续弹出,即使您移至另一个页面并且仅在单击按钮时停止?

html - CSS:更改段落的字体大小会更改 H1 的显示大小

javascript - 更改另一个 Div 上的文本

javascript - 如果我从桌面进行开发,我应该使用什么作为文件路径?

javascript - jQuery 点击事件目标填充