如何制作2种不同的滚动功能



你好,我做了2个脚本滚动,一个使导航条变小,第二个显示按钮进入页面的开始。当我添加第二个(按钮)我的第一个导航条没有工作。我删除了第二个,它工作了。我需要改变什么?

为导航条

window.onscroll = function() {
var el = document.getElementsByClassName('header')[0];
var className = 'small';
if (el.classList) {
if (window.scrollY > 10)
el.classList.add(className);
else
el.classList.remove(className);
}
};
window.smoothScroll = function(target) {
var scrollContainer = target;
do { //find scroll container
scrollContainer = scrollContainer.parentNode;
if (!scrollContainer) return;
scrollContainer.scrollTop += 1;
} while (scrollContainer.scrollTop == 0);
var targetY = 0;
do { //find the top of target relatively to the container
if (target == scrollContainer) break;
targetY += target.offsetTop;
} while (target = target.offsetParent);
scroll = function(c, a, b, i) {
i++; if (i > 30) return;
c.scrollTop = a + (b - a) / 30 * i;
setTimeout(function(){ scroll(c, a, b, i); }, 20);
}
// start scrolling
scroll(scrollContainer, scrollContainer.scrollTop, targetY, 0);
}

为按钮

//Get the button
var mybutton = document.getElementById("myBtn");
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll1 = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}

<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>  

<div class="header">
<div class="container">
<ul class="nav">
<li><a href="#">Home</a></li>
</ul>
</div>
</div>

只需注释按钮的第二个window.onscroll = function() {scrollFunction()};并将scrollFunction();添加到导航栏中window.onscroll,您的初始代码在window.onscroll1处有一个额外的1但您不需要设置两次,测试一下,我添加了一些CSS使按钮保持在右下角并设置一些高度以测试滚动

window.onscroll = function () {
scrollFunction();
var el = document.getElementsByClassName("header")[0];
var className = "small";
if (el.classList) {
if (window.scrollY > 10) el.classList.add(className);
else el.classList.remove(className);
}
};
window.smoothScroll = function (target) {
var scrollContainer = target;
do {
//find scroll container
scrollContainer = scrollContainer.parentNode;
if (!scrollContainer) return;
scrollContainer.scrollTop += 1;
} while (scrollContainer.scrollTop == 0);
var targetY = 0;
do {
//find the top of target relatively to the container
if (target == scrollContainer) break;
targetY += target.offsetTop;
} while ((target = target.offsetParent));
scroll = function (c, a, b, i) {
i++;
if (i > 30) return;
c.scrollTop = a + ((b - a) / 30) * i;
setTimeout(function () {
scroll(c, a, b, i);
}, 20);
};
// start scrolling
scroll(scrollContainer, scrollContainer.scrollTop, targetY, 0);
};
//Get the button
var mybutton = document.getElementById("myBtn");
// When the user scrolls down 20px from the top of the document, show the button
//window.onscroll1 = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
.container {
height: 1999px;
}
#myBtn  {
position: fixed;
bottom: 0;
right: 0;
display: none;
}
<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
<div class="header">
<div class="container">
<ul class="nav">
<li><a href="#">Home</a></li>
</ul>
</div>
</div>

我找到了另一个解决方案

像这样

var mybutton = document.getElementById("myBtn");

window.onscroll = function() {scrollFunction()};{
function scrollFunction() {
var el = document.getElementsByClassName('header')[0];
// Vrh button prikazivanje
var className = 'small';
if (el.classList) {
if (window.scrollY > 10)
el.classList.add(className);
else
el.classList.remove(className);
}
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
};
window.smoothScroll = function(target) {
var scrollContainer = target;
do { //find scroll container
scrollContainer = scrollContainer.parentNode;
if (!scrollContainer) return;
scrollContainer.scrollTop += 1;
} while (scrollContainer.scrollTop == 0);
var targetY = 0;
do { //find the top of target relatively to the container
if (target == scrollContainer) break;
targetY += target.offsetTop;
} while (target = target.offsetParent);
scroll = function(c, a, b, i) {
i++; if (i > 30) return;
c.scrollTop = a + (b - a) / 30 * i;
setTimeout(function(){ scroll(c, a, b, i); }, 20);
}
// start scrolling
scroll(scrollContainer, scrollContainer.scrollTop, targetY, 0);
}

相关内容

  • 没有找到相关文章

最新更新