Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.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/typo3/2.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 如何创建Facebook时间轴样式的日期滚动条?_Javascript_Jquery - Fatal编程技术网

Javascript 如何创建Facebook时间轴样式的日期滚动条?

Javascript 如何创建Facebook时间轴样式的日期滚动条?,javascript,jquery,Javascript,Jquery,我正在尝试复制Facebook时间线日期选择器/页面滚动器的功能。这可以在Facebook时间轴页面的右上角看到。当您使用动画效果选择一年时,页面将沿时间线向下滚动到该特定年份。以下是我一直试图为自己工作的代码: <script type="text/javascript"> $(document).ready(function(){ $("ul a").click(function(event){ if($(this)

我正在尝试复制Facebook时间线日期选择器/页面滚动器的功能。这可以在Facebook时间轴页面的右上角看到。当您使用动画效果选择一年时,页面将沿时间线向下滚动到该特定年份。以下是我一直试图为自己工作的代码:

    <script type="text/javascript">

    $(document).ready(function(){

        $("ul a").click(function(event){
              if($(this).hasClass("fourthlink")){
                        // get the coordinates of the fourth div
            var offset = $("#sect4").offset();
                        // alert the y coordinate; I'm getting the right coordinate: 1062 
            alert(offset.top);
                        // Here's where I'm trying to move the page to the right spot
                window.moveTo(offset.left,offset.top);
                        // I've also tired window.scrollTo(offset.left,offset.top);
            }
        })


    });
</script>

我要做的第一件事就是让寡妇滚动到正确的div。然后,我想添加一个类似Facebook的动画效果。

请将您的代码更正为:

$(document).ready(function(){
    $("ul a").click(function(event){
          if($(this).hasClass("fourthlink")){
                    // get the coordinates of the fourth div
        var offset = $("#sect4").offset();
                    // alert the y coordinate; I'm getting the right coordinate: 1062 
        alert(offset.top);
                    // Here's where I'm trying to move the page to the right spot
            window.scrollTo(offset.left,offset.top);
                    // I've also tired window.scorllTo(offset.left,offset.top);
        }
    })
});
您必须替换:window.moveTo到window.scrollTo。或者,如果这不起作用,请尝试以下方法:

$("html, body").animate({
    top: offset.top,
    left: offset.left
});

在做了更多的研究并使用了一些不同的搜索词之后,我偶然发现了一个解决了我的问题的方法,所以我想我会分享它。

你必须替换:window.moveTo to window.scrollTo。你可以检查一下,我在代码中评论说我也尝试了window.scrollTo,但这似乎也不起作用。好的,那样的话,试试这个$html,body.animate{top:offset.top,left:offset.left};那也没用。我只是打开并尝试在另一个浏览器,只是为了确保它,但没有运气。