[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

Control list scroll position on load.

This is a follow-up to my earlier question (#27928418).

The attachment to this question is a screen capture of a web page I am working on which contains an area where a list of songs is displayed. Also displayed is the music notation of a song which the user has selected. The music notation is in an iframe. Clicking on a song title loads its notation into the frame without reloading the parent page. The HTML source code is also attached. This page is the result of a previous page where the song list is displayed. When the user selects a song title, it links to this notation page where the song list is redisplayed alongside the musical notation of the selected song.

The song list can vary in length from one song to several hundred titles. Once the length of the list exceeds the height of the space allotted to it, the right scroll bar appears, allowing the user to scroll down to view additional songs on the list. The problem is the user may have selected a song far down the list. When the page loads, by default, the topmost portion of the list is displayed.

When this notation page loads, I would like the list to be scrolled to the position where the selected song title is at the top of the displayed titles. It should still be in its correct position relative to its neighboring song titles, but should appear at the top of the visible portion of the list, just as if it were scrolled to that position. The user could then scroll up or down from there to review additional titles. My client is adamant that I not use a paging system, so I must retain the scroll format. Also, the client likes the look as it is, so any solution should contain no additional controls (or at least minimize them).

The information about each song is contained within a div having and id of “key” plus the number equal to its position in the array of songs in the list, e.g. key23 is the id for the 24th song in the list.

Is there a way to do this?
Sample-Screenshot.png
CCS-Song-Library.htm
0
gpinzino
Asked:
gpinzino
  • 4
  • 3
2 Solutions
 
COBOLdinosaurCommented:
Well if you have an id then just target the id in the url and it will be positioned at the top.

http://mysite.com/mypage.html#key23 

will put the element with id 'key23' at the top.

Cd&
0
 
gpinzinoAuthor Commented:
Thanks COBOLdinosaur. It works well on the list. Unfortunately, it also pulls down the entire page so you do not see the top banner and navigation.  Is there a way to move the page back up so you can see what's at the top of the page?
0
 
COBOLdinosaurCommented:
Well you could try this:
http://clifgriffin.com/2008/10/14/using-javascript-to-scroll-to-a-specific-elementobject/

But I don't know how reliable it will be using an iframe.

You might be better to use window.scrollTo(0,0) in the main page after loading the iframe.

Cd&
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
gpinzinoAuthor Commented:
I was thinking the scrollTo might be an option here. I will check out both and let you know.
0
 
gpinzinoAuthor Commented:
I got it on the first try. Really quite simple.

In the <script>, I added:
function showIt(elID) {
      var el = document.getElementById(elID);
      el.scrollIntoView(true);
}

And then I modified my <body> tag to read:
<body id="bodyId" onload="showIt('bodyId')">

That's it. When the page loads, it jumps right to the top!
0
 
gpinzinoAuthor Commented:
Great job, COBOLdinosaur. Thanks!
0
 
COBOLdinosaurCommented:
:^) Great! I'm glad you have it working.

Cd&
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now