views:

63

answers:

1

I am trying to use jquery to do an animated scroll from one div to another. I am halfway there but can't seem to get it to scroll on both axis. I have called the scrollto plugin and am open to using that. Here is the site live and the code: http://littleboxcreative.com/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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>
+2  A: 

$.scrollTo has an awesome demo page: http://demos.flesler.com/jquery/scrollTo/

I noticed in your code above, you aren't actually using $.scrollTo, but instead animating scrollTop.

Instead of this:

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

I would use the following, making $target the element you are trying to scroll to (you don't have to worry about calculating its offset, and I think the default is to scroll on both axes):

$.scrollTo($target, 1000);

Or, if you want to be more verbose:

$.scrollTo( $target, { duration: 1000, axis: 'xy' } );
JKS
when i make this replacement i lose the animation it just jumps right to the div.
Davey
I made the replacement, and it works as expected: http://jsfiddle.net/khghQ/
JKS
Ah, there it is! Thanks dude.
Davey