2017-04-08 117 views
0

鉴于以下按钮:向下滚动使用按钮

<section id="section04" class="demo"> 
    <h1>Scroll Down Button #4</h1> 
    <a><span></span>Scroll</a> 
    </section> 

我怎样才能使按钮向下滚动页面上有一个特定的金额是多少?

+3

的[上下滚动使用jQuery的按钮,点击一个div]可能的复制(HTTP:/ /stackoverflow.com/questions/16586594/scroll-up-and-down-a-div-on-button-click-using-jquery) –

回答

0

使用jQuery动画

$("#section04").click(function(){ 
    $("html, body").stop().animate({ 
    scrollTop: 100px 
    },1000); 
}); 

更改100像素所需的任何值。

+0

我应该把这个文件准备好吗? – user7678931

+0

是的,你必须把这些代码放在文档就绪函数中。 –

0

如果你想使用香草的JavaScript,你可以使用内置的方法scrollTo

窗口
//.html 

    <div style='height:4000px; border:solid red 2px; width:80%;'> 
     <input type='text' class='y' /> 
     <button>Scroll Y-axis</button> 
    </div> 

//.js 

    var button = document.querySelector('button') 

    button.addEventListener('click', function(){ 
     var y = document.querySelector('.y').value 
      window.scrollTo(0,y) 
    }) 

jsfiddle