作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在使用 jquery 来实现 Codepen 中的一些基本平滑滚动功能,除了 codepen 中的 navbar-shrink 转换之外,一切似乎都有效。所以我决定在 Visual Studio Code Editor 中尝试一下,结果出了问题。似乎我的 jquery 代码根本无法正常工作,即使我检查了 html 中的文件链接,我认为所有内容都已链接在一起,我没有更改任何代码。为什么我的 jquery 代码根本不起作用。我从 bootstrap 4 起始模板开始,我可能做错了什么,任何人都可以看看我的代码并给我一些反馈,不胜感激。
$(document).ready(function () {
var scrollLink = $('.scroll');
scrollLink.click(function (e) {
e.preventDefault();
$('html, body').animate({
scrollTop: $(this.hash).offset().top
}, 900)
})
$(window).scroll(function () {
var scrollbarLocation = $(this).scrollTop();
if ($('.navbar').offset().top > 100) {
$('.navbar').addClass('navbar-shrink');
} else {
$('.navbar').removeClass('navbar-shrink');
}
scrollLink.each(function () {
var sectionOffset = $(this.hash).offset().top;
if (sectionOffset <= scrollbarLocation) {
$(this).parent().addClass('active');
$(this).parent().siblings().removeClass('active');
}
})
})
})
section {
width: 100%;
height: 900px;
}
#home {
background-color: lightblue;
}
#about {
background-color: lightsalmon;
}
#contact {
background-color: lightgray;
}
.active {
background-color: blue !important;
color: #FFF !important;
}
* {
transition: all .4s ease-in 0s;
}
.navbar-shrink {
height: 45px;
/* background-color: transparent !important; */
}
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
<title>Hello, world!</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top transition">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link scroll" href="#home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link scroll" href="#about">About</a>
</li>
<li class="nav-item">
<a class="nav-link scroll" href="#contact">Contact</a>
</li>
</ul>
</div>
</nav>
<section id='home'>Home</section>
<section id='about'>About</section>
<section id='contact'>Contact</section>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script src='main.js'>
</script>
</body>
</html>
最佳答案
关于javascript - jQuery 代码在链接到 html 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54754910/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!