jquery动画在x和y轴上滚动

时间:2010-10-03 21:35:51

标签: javascript jquery html css

我正在尝试使用jquery从一个div到另一个div进行动画滚动。我在那里,但似乎无法让它在两个轴上滚动。我已经调用了scrollto插件,并且可以使用它。这是网站直播和代码:http://littleboxcreative.com/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Little Box Creative</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="jqtransformplugin/jqtransform.css" />
<link rel="stylesheet" type="text/css" href="formValidator/validationEngine.jquery.css" />
<link rel="stylesheet" type="text/css" href="demo.css" />




<script type="text/javascript" src="js/jquery.jqtransform.js"></script>
<script type="text/javascript" src="js/jquery.validationEngine.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="/js/jquery-ui.js"></script>
<script type="text/javascript" src="/js/jquery-scrollTo.js"></script>

<script type="text/javascript">
$(document).ready(function(){
  $('a[href*=#]').click(function() {
 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
 && location.hostname == this.hostname) {
   var $target = $(this.hash);
   $target = $target.length && $target
   || $('[name=' + this.hash.slice(1) +']');
   if ($target.length) {
  var targetOffset = $target.offset().top;
  $('html,body')
  .animate({scrollTop: targetOffset}, 1000);
    return false;
   }
 }
  });
});
</script>


</head>




<body>



<div id="main">
    <div id="mainwrap1">
    <div id="logo">
    </div>
    <div id="tagline">

        <div class="tagline1">
        </div>
    <div id="headline">
    </div>
    <a href="#mainwrap2">
        <div class="tagline2">
        </div>
    </a>    




    </div>
    </div>

    <div id="mainwrap2">





    </div>
</div>



<div id="body">
        <div id="services">

        </div>

        <div id="portfolio">

        </div>
    <div id="innerbody">
        <div id="welove">

        </div>
    </div>
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:3)

$。scrollTo有一个很棒的演示页面: http://demos.flesler.com/jquery/scrollTo/

我注意到在上面的代码中,你实际上并没有使用$ .scrollTo,而是使用scrollTop动画。

而不是:

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

我会使用以下内容,使$target成为您要尝试滚动的元素(您不必担心计算其偏移量,我认为默认是在两个轴上滚动):< / p>

$.scrollTo($target, 1000);

或者,如果你想要更详细:

$.scrollTo( $target, { duration: 1000, axis: 'xy' } );