我在做一个角项目。我想添加一个导航栏,它最初有透明的背景,但在滚动它会改变它的颜色。我正在使用引导类
我的导航栏标题是html代码:
<nav class="navbar navbar-expand-md sticky-top">
...
</nav>
我可以在哪里添加脚本以更改其滚动上的颜色
定义一个scroll事件就像定义一个onscroll
属性一样简单,比如
<nav class="navbar navbar-expand-md sticky-top" onscroll="myScroll()">
让我们实现myScroll
。卷轴是一个连续的事件,棘手的部分是抓住它的结束。为此,我们可以执行setInterval
:
var scrollInterval = undefined;
var lastScroll = false;
function myScroll() {
lastScroll = true;
if (!scrollInterval) {
//scroll has started, do some coloring
lastScroll = false;
scrollInterval = setTimeout(function() {
if (!lastScroll) {
//scroll has ended, revert the coloring
scrollInterval = clearInterval(scrollInterval);
}
lastScroll = false;
}, 100);
} else {
lastScroll = true;
}
}
$(function () {
$(document).scroll(function () {
var $nav = $(".navbar-fixed-top");
$nav.toggleClass('scrolled', $(this).scrollTop() > $nav.height());
});
});
@import "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css";
body {
padding-top: 70px;
background: #ddd;
}
.navbar-fixed-top.scrolled {
background-color: #fff !important;
transition: background-color 800ms linear;
}
.navbar-fixed-top.scrolled .nav-link {
color:#555;
}
html lang-html prettyprint-override"><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<nav id="navigation" class="navbar navbar-dark bg-primary navbar-fixed-top">
<ul class="nav navbar-nav">
<li class="nav-item"><a href="#" class="nav-link">Home</a></li>
<li class="nav-item"><a href="#projects" class="nav-link">Teams</a></li>
<li class="nav-item"><a href="#blog" class="nav-link">Service</a></li>
<li class="nav-item"><a href="#contact-us" class="nav-link">Contact us</a></li>
</ul>
</nav>
</header>
<h1>Folow</h1>
<p>
"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
</p>
您可以在Typescript
文件中使用@HostListner
实现这一点。
import { HostListener } from @angular/core;
@HostListener('window:scroll', ['$event'])
onWindowScroll() {
let element = document.querySelector('.navbar') as HTMLElement;
if (window.pageYOffset > element.clientHeight) {
element.classList.add('navbar-inverse');
} else {
element.classList.remove('navbar-inverse');
}
}
将滚动事件放在超文本标记语言部分。
<nav class="navbar navbar-expand-md sticky-top" (scroll)="onWindowScroll();"></nav>
如何设置没有背景色的导航栏? 当在div之后向下滚动导航条时,导航条会得到一个新的背景颜色(导航条应该固定在顶部,我在引导中使用) 我尝试过一些教程,但没有成功。 这是网站:http://attafothman.olympe.in/ 我说的是上面那个黑色导航条。
我的导航栏有一个白色背景,但在登录页上它应该是透明的,当我向下滚动时它应该是白色的,在其他页面上它应该是白色的。 我使用的代码来自:滚动后更改导航栏颜色? 编辑: 所以我在下面的答案中添加了一个小提琴,但不知何故它不起作用 https://jsfiddle.net/jy6njukm/ 这是我的代码: javascript: 这里是我的navbar css: 我有我的导航栏html只有 我的home
我想要一个半透明的状态栏和导航栏,其他颜色不能像蓝色或白色那样半透明 我的代码 活动 状态栏半透明良好,但导航栏颜色不变。为什么? 导航栏
当我滚动页面时,我很难让固定顶部导航栏更改背景颜色。 以下是JS中的函数: 这是一个名为“”的文件,在我加载后加载到页面底部(与位于同一文件夹中) 这里是html导航栏: 然后,我只有css用于更改导航栏背景颜色:
我使用引导为一个主题,我看到了这个网站:http://www.luatix.org/en/,我喜欢navbar上的效果。向下滚动时更改颜色并更改元素的颜色。 谢啦
我想让固定导航栏在页面滚动时,当它超过明亮的背景颜色时,它的背景颜色变为深色。例如,当它超过白色背景时,它应该变为黑色,反之亦然。 html 该Css