首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery滚动到不使用sweetalert.js的元素

jQuery滚动到不使用sweetalert.js的元素
EN

Stack Overflow用户
提问于 2015-12-16 04:04:06
回答 1查看 2.7K关注 0票数 0

单击按钮时,我想滚动页面。下面的代码在没有sweetalert.js和sweetalert.css的情况下工作得很好。首先,页面滚动到顶部,但当我单击“警报”框中的“确定”按钮时,页面会自动向下滚动。请从sweetalert.js链接下载和sweetalert.css。

代码语言:javascript
复制
<html>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"> </script>
        <script src="js/sweetalert.min.js"></script>
    <link rel="stylesheet"  type="text/css" href="css/sweetalert.css"/>
    <script>
$(document).ready(function (){
    $("#click").click(function (){

        $('html, body').animate({
                    scrollTop: $("#div1").offset().top
                }, 2000);

        swal({
            title: "This is title",
            text: "Some error occurred",
            type: "error",
            confirmButtonText: "Ok"
        });
        window.scrollTo(0, 100);
            });
});
    </script>
    <div id="div1" style="height: 900px; width 100px">
    Div1 is here.
    </div>
    <br/>
    <div id="div2" style="height: 900px; width 100px">
    Div2 is here.
</div>
    <button id="click">Click here</button>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-16 05:22:08

我不知道你的产出需要多少。但是,把你的滚动在确认回调,所以页面将滚动时,用户点击甜警报康弗里姆警报。以下是已编辑的代码。

代码语言:javascript
复制
$(document).ready(function (){
    $("#click").click(function (){
        swal({
            title: "This is title",
            text: "Some error occurred",
            type: "error",
            confirmButtonText: "Ok"
        }, function(){
            $('html, body').animate({
                    scrollTop: $("#div1").offset().top
                }, 2000);
            window.scrollTo(0, 100);
        });
    });
});

另一种方法是在点击后将其保持在顶部。

代码语言:javascript
复制
$(document).ready(function (){
    $("#click").click(function (){
        $('html, body').animate({
                    scrollTop: $("#div1").offset().top
                }, 2000);

        swal({
            title: "This is title",
            text: "Some error occurred",
            type: "error",
            confirmButtonText: "Ok"
        }, function(){
            $('html, body').animate({
                    scrollTop: $("#div1").offset().top
                }, 100);
        });
    });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34303736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档