Results 1 to 6 of 6

Thread: hash tags slide to div

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default hash tags slide to div

    i have a hash tag on a div, to view it you need to scroll down.
    from a button click, how do i get this code to scroll to that div rather than just jump to its location?
    Code:
    <!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" />
    <style>
    #topDiv{height:900px; width:600px; display:block; background-color:#ccc;}
    #midDiv{height:600px; width:600px; display:block; background-color:#c99;}
    a.btn1{cursor:pointer;}
    
    </style>
    <script src="http://code.jquery.com/jquery-1.5.min.js"></script>
    
    <script>
    $(document).ready(function() {
    	$("a.btn1").click(function() {
    		window.location.hash = 'midDiv'; 
    		$("#midDiv").slideDown(2000);
    	});
    });
    </script>
    </head>
    <body>
    <div id="nav">
    	<ul>
    		<li><a class="btn1">btn 1</a></li>
    	</ul>
    </div>
    <div id="topDiv"></div>
    <div id="midDiv"></div>
    </body>
    </html>
    Last edited by ggalan; 03-28-2011 at 02:53 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

  3. #3
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    hmm, having issues. still doesnt work
    Code:
    <!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" />
    <style>
    #topDiv{height:900px; width:600px; display:block; background-color:#ccc;}
    #midDiv{height:600px; width:600px; display:block; background-color:#c99;}
    a.btn1{cursor:pointer;}
    
    </style>
    <script src="http://code.jquery.com/jquery-1.5.min.js"></script>
    <script type='text/javascript' src='jquery.scrollTo-min.js'></script>
    
    <script>
    $(document).ready(function() {
    
    });
    </script>
    </head>
    <body>
    <div id="nav">
    	<ul>
    		<li><a class="btn1" title="$(...).scrollTo( $('#midDiv'), 800 );">btn 1</a></li>
    	</ul>
    </div>
    <div id="topDiv"></div>
    <div id="midDiv"></div>
    </body>
    </html>

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Try this

    Code:
    <!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" />
    <style>
    #topDiv{height:900px; width:600px; display:block; background-color:#ccc;}
    #midDiv{height:600px; width:600px; display:block; background-color:#c99;}
    a.btn1{cursor:pointer;}
    
    </style>
    <script src="http://code.jquery.com/jquery-1.5.min.js"></script>
    <script type='text/javascript' src='jquery.scrollTo-min.js'></script>
    <script type="text/javascript">
    $('.btn1').live('click', function () {
    	$('html,body').animate({scrollTop: $("#midDiv").offset().top},'slow');
    });
    </script>
    </head>
    <body>
    <div id="nav">
    	<ul>
    		<li><a class="btn1">btn 1</a></li>
    	</ul>
    </div>
    <div id="topDiv"></div>
    <div id="midDiv"></div>
    </body>
    </html>
    Corrections to my coding/thoughts welcome.

  5. #5
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    thanks!
    if i wanted to add a hash mark to the url so that you could link into the div by placing this code the animate breaks, any suggestions?

    Code:
    $('.btn1').live('click', function () {
    		window.location.hash = '#midDiv'; 
    		$('html,body').animate({scrollTop: $("#midDiv").offset().top},'slow');
    	});

  6. #6
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    nm, just rearranged it

    Code:
    $('.btn1').live('click', function () {
    		$('html,body').animate({scrollTop: $("#midDiv").offset().top},'slow');
    		window.location.hash = '#midDiv'; 
    	});

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •