SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ggle forcing page to return to top

    I am using the js toggle script:

    Code:
    <script language="JavaScript">
    	// <![CDATA[
    	function toggle(objId, togId) {
    	  var o = document.getElementById(objId), t = document.getElementById(togId);
    	  if (o.style.display == 'none') {
    	    o.style.display = 'block';
    	    t.innerHTML = '<a href="#"><b>Hide</b></a>';
    	  } else {
    	    o.style.display = 'none';
    	    t.innerHTML = '<a href="#"><b>Show</b></a>';
    	  }
    	}
    	// ]]>
    </script>
    And i have a link that shows/hides a div, on every row for the entire page. This works well, aprt from the fact that everytime i hit the show/hide link, it returns the browser to the top.

    Is there an way to avoid this, as it will be annoying from a user point-of-view?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,140
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    toggle back to top

    It must be that the changing from none <-> block forces the browser to render the page anew. Maybe you could get the Y offset and go back to it after the toggle?

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any chance you could show me the way...

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,140
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    Yoffset

    From http://developer.mozilla.org/en/docs..._DOM_Reference
    Maybe something like this at the beginning of the function
    Code:
    var vScroll = pageYOffset;
    and then use that variable at the end of the function with
    Code:
    window.moveTo(0, 0)
    using the vScroll value for the X parameter

  5. #5
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    <a href="#" onclick="return false">Show / Hide</a>

  6. #6
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That did the job, cheers


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
  •