我正在创建一个向下滚动按钮,也正在创建一个向上滚动按钮。
我已经创建了两个按钮,但是当我单击它们中的任何一个时,它们都没有滚动,我也不知道为什么。我已经尝试了一切,但仍然无法正常工作。我什至使用了一个代码,该代码在我开发的其他网站上也能很好地工作,但仍然没有。
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" name="viewport" />
<!--<link rel="shortcut icon" href="icons/favicon.ico" type="image/x-icon">
<link rel="icon" href="icons/favicon.ico" type="image/x-icon">-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link title="main_css" rel="stylesheet" href="css/main.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous">
</script-->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">
</script>
<script src="js/main.js" type="text/javascript"></script>
</head>
<body style="background:#FFF !important;">
<a id="button"></a> <!-- THIS IS THE SCROLL TO TOP BUTTON -->
<!--NAVIGATION AND HEADER SECTION-->
<nav class="navbar about-navbar">
<a href="index.html" class="navbar-brand">
<img class="logo-desktop" src="icons/logo-white.svg">
<img class="logo-small" src="icons/logo-white.svg">
</a>
<ul class="nav justify-content-end">
<li class="nav-item about-nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item about-nav-item">
<a class="nav-link active" href="about.html">About</a>
</li>
<li class="nav-item about-nav-item">
<a class="nav-link" href="gallery.html">Gallery</a>
</li>
<li class="nav-item about-nav-item">
<a class="nav-link" href="contact.html">Contact</a>
</li>
<div class="push-button">
<div class="top-line"></div>
<div class="middle-line"></div>
<div class="middle-line under"></div>
<div class="bottom-line"></div>
</div>
</ul>
</nav>
<div class="container-fluid header-section about-header-section">
<div class="about-bg"></div>
<div class="inside-element">
<div class="col-12 col-lg-8 about-section-text">
<div class="main-heading-text-box about-main-box">
<h1 class="font-weight-light">We <span style="color: #16E1F5;">care</span> about people</h1>
<h5 class="lead">Give a helping hand for poor people</h5>
<a href="donate.html" class="about-donate-btn donate_button">
<p class="first">Donate</p>
<p class="second">Donate</p>
<img src="icons/right_white.svg" alt="">
</a>
</div>
</div>
<!-- THIS IS THE SCROLL DOWN BUTTON -->
<div class="scroll-down">Scroll</div>
<div class="vertical-divider"></div>
</div>
</div>
下面是main.js中的代码:
$(document).ready(function () {
$(".scroll-down").click(function () {
return $("html, body").animate({
scrollTop: $("#about").offset().top - 64
}, 800);
});
var $document = $(document),
$element1 = $('#button'),
changed = 'show';
if ($document.scrollTop() >= 500) {
$element1.removeClass(changed);
} else {
$element1.addClass(changed);
}
$(window).scroll(function () {
var $document = $(document),
$element1 = $('#button'),
changed = 'show';
if ($document.scrollTop() >= 150) {
$element1.removeClass(changed);
} else {
$element1.addClass(changed);
}
});
$('#button').click(function () {
$("html, body").animate({ scrollTop: 0 }, 100);
return false;
});
});
答案 0 :(得分:0)
我不确定您是否可以同时为html和body设置动画?您为什么不给div一个可靠的ID并为其设置动画效果,该效果应该适合您。
答案 1 :(得分:0)
我怀疑您正在尝试遵循:Is it possible to animate scrollTop with jQuery?
请参阅:https://jsfiddle.net/Twisty/e3x42Lnm/
使用动画时,您想要设置一个像素值以滚动到:
$('#button').click(function(e) {
e.preventDefault();
log("Button Click");
$("html, body").animate({
scrollTop: "0px"
}, 500, function() {
log("Scroll Animation Complete")
});
});
似乎缺少许多要素。因此,我的Fiddle只是一个示例,需要根据您的特定代码进行调整。以后,请提供一个Minimal, Complete, and Verifiable example。
希望有帮助。