我找到了一个很棒的纯javascript代码,用于平滑滚动功能,但我不知道如何让它在onclick内联函数中工作。 我确定它的东西很简单,但是javascript非常弱......
<div class="navbar">
<button type="button" id="clickme">scroll whit class</button>
<button type="button" onclick="smoothScroll(third)" >not working yet</button>
</div>
<div class="third" id="third">The start of the red section!</div>
和smoothScroll函数javascript:
var targetY;
document.getElementById('clickme').addEventListener('click', function() {
smoothScroll(document.getElementById('third'));
});
window.smoothScroll = function(target) {
var scrollContainer = target;
headerSpace();
do {
scrollContainer = scrollContainer.parentNode;
if (!scrollContainer) return;
scrollContainer.scrollTop += 1;
}
while (scrollContainer.scrollTop == 0);
do {
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);
}
scroll(scrollContainer, scrollContainer.scrollTop, targetY, 0);
}
function headerSpace() {
var header = document.querySelectorAll('.navbar');
targetY = -header[0].clientHeight;
}
正如您所看到的,所有已准备好的getElement都是id,但我想将其删除并使其仅使用白色内联
onclick="smoothScroll(second)"
onclick="smoothScroll(third)"
onclick="smoothScroll(fourth)"
等...... 这是jsFiddle
答案 0 :(得分:2)
var scrollContainer = document.getElementById(target);
target = document.getElementById(target);
然后在你的HTML中
<button type="button" onclick="smoothScroll('third')" id="clickme">scroll whit onclick=""</button>
答案 1 :(得分:0)
尝试使用这个将帮助您顺利滚动,这太容易使用
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
您需要的只是拥有一个链接
<a href="#third">not working yet</a>
<div id="third"></div>
这将与你绑定到任何div(href(用于链接)和id(对于div))的任何链接顺利地工作)小心这两个属性,这将工作正常
您也可以在以下链接中找到演示考试将向您解释的更多
答案 2 :(得分:0)
您的小提琴与您的问题中提供的代码不同。假设您的小提琴是最新版本,它不起作用,因为您有两个具有相同ID的按钮,根据HTML规范无效
<button type="button" id="clickme">scroll whit class</button>
<button type="button" id="clickme">scroll whit onclick=""</button>
因此,事件监听器仅添加到第一个按钮,因为getElementById
只返回一个元素:
document.getElementById('clickme').addEventListener('click', function() {
您可以通过向第二个按钮添加不同的ID来解决此问题:
<button type="button" id="clickme">scroll whit class</button>
<button type="button" id="clickme1">scroll whit onclick=""</button>
并添加第二个监听器:
function scrollDiv()
{
var header = document.querySelectorAll('.navbar');
aim = -header[0].clientHeight;
initial = Date.now();
smoothScroll(document.getElementById('third'));
}
document.getElementById('clickme').addEventListener('click', scrollDiv)
document.getElementById('clickme1').addEventListener('click', scrollDiv)
或者稍微清洁一点,你可以在按钮上添加一个类并迭代结果document.getElementsByClassName