我正在为学校做一个元素,我的 Javascript 突然停止显示。谁能告诉我发生了什么事?
我说的是应该出现在右下角的滚动回到顶部按钮。
jsfiddle:https://jsfiddle.net/u67cea52/
$(document).ready(function() {
function showItem(evt, itemName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(itemName).style.display = "block";
evt.currentTarget.className += " active";
}
$(document).ready(function(){
//kijkt of je verder bent dan de cover pagina, zoja dan verschijnt de knop
$(window).scroll(function(){
if ($(this).scrollTop() > 100) {
$('.scrollToTop').fadeIn();
} else {
$('.scrollToTop').fadeOut();
}
});
//het event wat ervoor zorgt dat als je op de knop klikt dat je weer naar boven gaat
$('.scrollToTop').click(function(){
$('html, body').animate({scrollTop : 0},800);
return false;
});
});
});
最佳答案
这可能会有所帮助:
HTML:
<div id="content">Hello World</div>
<a href="#" id="back-to-top" title="Back to top">↑</a>
CSS:
#back-to-top {
position: fixed;
bottom: 40px;
right: 40px;
z-index: 9999;
width: 32px;
height: 32px;
text-align: center;
line-height: 30px;
background: #f5f5f5;
color: #444;
cursor: pointer;
border: 0;
border-radius: 2px;
text-decoration: none;
transition: opacity 0.2s ease-out;
opacity: 0;
}
#back-to-top:hover {
background: #e9ebec;
}
#back-to-top.show {
opacity: 1;
}
#content {
height: 2000px;
}
JS:
if ($('#back-to-top').length) {
var scrollTrigger = 100, // px
backToTop = function () {
var scrollTop = $(window).scrollTop();
if (scrollTop > scrollTrigger) {
$('#back-to-top').addClass('show');
} else {
$('#back-to-top').removeClass('show');
}
};
backToTop();
$(window).on('scroll', function () {
backToTop();
});
$('#back-to-top').on('click', function (e) {
e.preventDefault();
$('html,body').animate({
scrollTop: 0
}, 700);
});
}
关于Javascript 函数未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43134401/