Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击按钮后,将元素滚动到顶部_Javascript_Php_Html_Css - Fatal编程技术网

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

Javascript 单击按钮后,将元素滚动到顶部,javascript,php,html,css,Javascript,Php,Html,Css,我想在我的页面上添加“添加评论”按钮。 所以,单击此处,我将显示表单以添加注释。 我希望表单可以滚动到页面的顶部位置,这样用户就不需要向下滚动就能看到所有的评论 <script>function showDiv() { var div = document.getElementById('comdiv'); var button = document.getElementById('button'); if (

我想在我的页面上添加“添加评论”按钮。 所以,单击此处,我将显示表单以添加注释。 我希望表单可以滚动到页面的顶部位置,这样用户就不需要向下滚动就能看到所有的评论

<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>`
函数showDiv(){
var div=document.getElementById('comdiv');
var button=document.getElementById('button');
如果(div.style.display!=“无”){
div.style.display=“无”;
button.value=“添加注释”;
}
否则{
button.value=“隐藏注释”;
div.style.display=“块”;
}
}`
评论

评论至少应有20个字符
发表评论 `
在按钮中添加onclick=“goTop()”

定义

<script>
function goTop(){
formName.submit(); // formName = the name property of the form you are submitting
window.scrollTo(0, 0);
</script>

函数goTop(){
formName.submit();//formName=正在提交的表单的name属性
滚动到(0,0);

您可以在div中添加一个ID并使用“scrollTop”属性

请看以下链接:

$(“按钮”)。单击(函数(){
$('html,body')。设置动画({
滚动顶部:“0px”
});
});
li{
列表样式:无;
}

  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
  • 评论1
注释
在“脚本”标记中使用此代码

$(“#按钮”)。单击(函数(){


})你的问题是什么?我没有投你反对票。因为我的名声不够。我想是其他人回答的。我是Stackoverflow新手,我不知道如何正确格式化,这可能就是他们这么做的原因。没关系,极客,欢迎来到Stackoverflow
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;
    };
};
   $('html,body').animate({scrollTop: '0px'}, 1000);