gpt4 book ai didi

javascript - bootstrap 4 - 导航栏品牌标志在向下滚动时不会改变

转载 作者:行者123 更新时间:2023-12-02 22:09:15 26 4
gpt4 key购买 nike

我添加了 bootstrap 4 并创建了导航栏,它运行良好,但我遇到了一些冲突,当向下滚动更改导航栏 Logo 时,我尝试进行更改,但它无法正常工作,任何人都知道如何解决该问题。

这是我的代码:

$(function () {
$(window).scroll(function () {
if ($(this).scrollTop() > 1000) {
$('.navbar .navbar-brand img').attr('src','https://via.placeholder.com/150/FF00FF/808080?Text=newLogo');
}
if ($(this).scrollTop() < 1000) {
$('.navbar .navbar-brand img').attr('src','https://via.placeholder.com/150/0000FF/808080?Text=oldLogo');
}
})
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<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>


<header class="main_menu home_menu">
<div class="container">
<div class="row align-items-center">
<div class="col-lg-12">
<nav class="navbar navbar-expand-lg navbar-light">
<a class="navbar-brand" > <img src="https://via.placeholder.com/150/0000FF/808080?Text=oldLogo" style="width:50px;"> </a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button></nav>
</div></div></div></header>

<section>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div> <div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div> <div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
</section>

最佳答案

查看下面的 fiddle 。它运行良好。只是您的图像隐藏在滚动下,否则您的图像会发生变化。

请注意,出于说明目的,我修复了您的导航栏 header 。

只需为您的导航栏使用 navbar navbar-expand-lg navbar-lightfixed-top 类,它就应该按预期工作。

$(function () {
$(window).scroll(function () {
if ($(this).scrollTop() > 1000) {
$('.navbar .navbar-brand img').attr('src','https://via.placeholder.com/150/FF00FF/808080?Text=newLogo');
}
if ($(this).scrollTop() < 1000) {
$('.navbar .navbar-brand img').attr('src','https://via.placeholder.com/150/0000FF/808080?Text=oldLogo');
}
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<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>


<header class="main_menu home_menu">
<div class="container">
<div class="row align-items-center">
<div class="col-lg-12">
<nav class="navbar navbar-expand-lg navbar-light fixed-top">
<a class="navbar-brand" > <img src="https://via.placeholder.com/150/0000FF/808080?Text=oldLogo" style="width:50px;"> </a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button></nav>
</div></div></div></header>

<section>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
<div>Placeholder</div>
</section>

关于javascript - bootstrap 4 - 导航栏品牌标志在向下滚动时不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59610953/

26 4 0