单击按钮,将元素滚动到顶部

时间:2015-07-22 11:36:39

标签: javascript php html css

我想在我的页面上添加“ADD COMMENT”按钮。 所以onClick我正在显示表单来添加评论。 我希望表单在页面的顶部位置滚动,因此用户无需向下滚动即可查看所有注释。

<script>function showDiv() {

            var div = document.getElementById('comdiv');
            var button = document.getElementById('button');
            if (div.style.display !== "none") {
                div.style.display = "none";
                button.value = "Add Comments";
            }
            else {
                button.value = "Hide Comments";
                div.style.display = "block";
            }
        }</script>`<input type="button" id="button" name="answer" value="Add Comments" onclick="showDiv()"  autofocus />
    <div id="comdiv"  style="display:none;" class="answer_list" >
        <form name="form" id="form" action="" method="post">
            <p class="h5">Comment</p>
            <textarea name="comment" id="comment" placeholder="Write Comment, Max: 140 characters" rows="4"  class="width-50" onblur="checkMe()" ></textarea>
            <span class="forms-desc">Comments should atleast have 20 character's</span><br/>

      <button type="submit" name="submit" id="submitbutton" class="btn btn-green btn-outline" disabled="disabled">Post Comment</button>
        </form></div>`

5 个答案:

答案 0 :(得分:1)

将此代码用于您的&#34;脚本&#34;标签

$(&#39;#button&#39;)。点击(function(){

   $('html,body').animate({scrollTop: '0px'}, 1000);

});

答案 1 :(得分:0)

按钮中的

添加onclick =&#34; goTop()&#34;

并定义

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Parent{
      private String name;
      private String surname;
}

答案 2 :(得分:0)

scrollIdIntoView("comment-1"); 
function scrollIdIntoView(id,speed) {
    if ($('#'+id).length) {
        $('html,body').animate({
            scrollTop : $('#'+id).offset().top - 30
        }, speed);
        $('#'+id).show().focus().click();
        return false;
    };
};

答案 3 :(得分:0)

$('button').click(function() {

  $('html, body').animate({
    scrollTop: '0px'
  });

});
li {
  list-style: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>
  <li>comment1</li>

</ul>

<button>Comment</button>

答案 4 :(得分:-2)

您可以在div中添加ID并使用&#39; scrollTop&#39;属性。

请看以下链接:

Similar question