所以我尝试在我的页面上创建一个按钮,动画并滚动回到页面顶部。我创建了一个名为upArrow的类,并将其添加到应该触发jQuery函数的按钮中。然而,当我点击什么火没有,我不知道为什么。这是网站和我的代码:
https://jorgeg1105.github.io/JG-Photography/
GRANT CREATE SESSION TO <<username>> WITH ADMIN OPTION
&#13;
//When the page loads, Fade in all divs with class hidden.
//Then Remove hidden class.
$(document).ready(function () {
$('div.hidden').fadeIn(2000).removeClass('hidden');
});
//Fade In all h3 with class hidden.
//Remove hidden class.
$('h3.hidden').fadeIn(3000).removeClass('hidden');
//Image Filter
$(document).ready(function () {
$("#mainnav ul li a").click(function (){
var category = $(this).attr("class");
if (category == "all" ){
$("#imgcontainer img").addClass("hidden");
setTimeout(function (){
$("#imgcontainer img").removeClass("hidden");
});
}
else {
$("#imgcontainer img").addClass("hidden");
setTimeout(function(){
$("."+category).removeClass("hidden");
});
}
});
});
//When burger is clicked. Mobile nav and mainnav are toggled.
$(".burger").on("click", function (){
$("#mainnav").toggleClass("hidden");
$(".mobilenav").toggleClass("hidden");
});
//When Arrow is clicked page scrolls to the top of the screen.
$('.upArrow').click(function(){
$('html, body').animate({scrollTop : 0},800);
return false;
});
&#13;
body {
margin: 0;
padding: 0;
display: flex;
height: 100vh;
overflow: hidden;
font-family: 'Raleway', sans-serif;
}
ul {
list-style-type: none;
}
#container {
flex: 1 0 0;
overflow-y: auto;
}
/*------------------------Classes to be added to the current active link on a page----------*/
.active {
color: #766E6B;
}
/*--------------Side Navigation Styles--------------------*/
#sidenav {
background-color: black;
color: white;
width: 60px;
text-align: center;
border-right: 6px solid #766E6B;
overflow-y: auto;
}
#sidenav ul {
display: flex;
flex-direction: column;
padding: 0;
margin: 0;
justify-content: center;
}
#sidenav ul li:first-child {
margin-bottom: auto;
}
#sidenav ul li:last-child {
margin-top: auto;
}
#sidenav a {
padding: 20px;
display: block;
}
#sidenav a:visited {
color: white;
}
#sidenav a:hover {
color: black;
background-color: white;
}
/*-------------Header Styles-------------------------------*/
header {
padding: 40px 30px;
background-color: #F7F6F2;
}
header h1, h3 {
font-family: 'Tangerine', cursive;
}
header h1 {
font-size: 90px;
}
header h3 {
font-size: 40px;
}
header a {
text-decoration: none;
color: black;
padding: 0 0 12px 0;
line-height: 1.5em;
}
header a:hover {
transition: color 1s;
color: #766E6B;
}
.smalltxt {
font-size: 12px;
}
.topnavitems {
padding: 20px;
position: relative;
}
/*------------------Main Navigation-----------------------*/
#mainnav {
display: flex;
justify-content: center;
background-color: black;
}
#mainnav li {
margin-right: 5px;
}
#mainnav a {
color: white;
text-decoration: none;
letter-spacing: 1px;
padding: 10px;
}
#mainnav a:hover {
font-size: 20px;
color: grey;
}
/*--------------------ImgContainer Area------------------------*/
#imgcontainer {
display: flex;
flex-direction: row;
flex-wrap: wrap;
padding: 30px 20px;
justify-content: center;
background-color: #F7F6F2;
}
#imgcontainer img {
flex: 1;
width: 40vh;
padding: 10px;
}
#imgcontainer img:hover {
border: 2px solid black;
}
/*-----------------------Footer Styles---------------------*/
footer {
background-color: #F7F6F2;
padding: 10px 20px;
border-top: 1px solid grey;
}
footer ul {
justify-content: center;
}
footer li {
margin-right: 10px;
}
.developer {
text-decoration: none;
color: black;
}
/*-------------------Js Fade In class-------------------*/
.hidden {
display: none !important;
}
/*-------------------Flexbox-----------------------*/
.col {
flex: 1;
}
.row {
display: flex;
}
/*----------------------Mobile Navigation------------------------*/
.mobilenav {
background-color: black;
justify-content: center;
display: none;
}
.mobilenav ul {
display: flex;
flex-direction: row;
margin: 0;
}
.mobilenav li {
margin-right: 10px;
}
.mobilenav a {
color: white;
text-decoration: none;
display: block;
padding: 20px;
}
.mobilenav a:hover {
background-color: grey;
}
.burger {
background-color: black;
width: 100%;
text-align: right;
display: none;
cursor: pointer;
}
.burger i {
color: white;
padding: 5px;
margin-right: 10px;
}
/*-----------------Mobile Footer------------------------------*/
.mobilefooter {
display: none;
}
.mobilefooter a {
color: black;
}
/*-----------Media Queries----------------------------*/
@media screen and (max-width: 1024px){
#sidenav {
display: none;
}
nav.mobilenav.row {
display: none;
}
.mobilefooter {
display: flex;
}
}
@media screen and (max-width: 930px){
#imgcontainer img {
width: 80vh;
}
}
@media screen and (max-width: 740px){
header a {
display: none;
}
.mobilenav {
display: flex;
}
}
@media screen and (max-width: 600px){
header h1 {
font-size: 70px;
}
header h3 {
font-size: 40px;
}
#imgcontainer img {
width: 95%;
}
#mainnav {
display: block;
}
#mainnav ul {
flex-direction: column;
width: 100%;
margin: 0;
text-align: center;
padding: 0;
}
#mainnav li {
border-bottom: 1px solid grey;
padding: 10px;
}
.mobilenav {
display: block;
}
.mobilenav ul {
flex-direction: column;
width: 100%;
margin: 0;
text-align: center;
padding: 0;
}
.mobilenav li {
border-bottom: 1px solid grey;
}
.burger {
display: block;
}
}
&#13;
感谢您的帮助!
答案 0 :(得分:2)
您需要禁用与向上箭头链接关联的eventDefault。 event.preventDefault()方法停止发生元素的默认操作,在我们的例子中,这意味着它阻止链接跟随'#'URL。
$('#back-to-top').on('click', function (e) {
e.preventDefault();
$('html,body').animate({
scrollTop: 0
}, 700);
看看this小提琴我作为一个例子。
希望这可以提供帮助, 劳拉。