Scrolling layers script help

I’m using scrolling layers to manage a long menu here:
http://www.lwbranddesign.com/lwpackagedesign.html

The problem, of course, is that the menu snaps back to the beginning on each page. I need to figure out a way to automatically scroll down on page load so the current location remains visible in the menu if you select one of the lower pages. I couldn’t make it work with a timer. Any suggestions?


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Anybody? A cricket chirps as tumbleweeds drift by…

:frowning:


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I looked at this the other day when you posted it, but then a tumbleweed drifted by and I got distracted.

Basically, what you need here is a way to get the top offset property of the item you want to focus, and then set that to be visible by modifying the style.top property of your inner (scrolling) DIV.

So on the Perfect Glue page, this little ditty does the trick:

$('PackageMenuContent1').setStyle({top:-$$('#PackageMenuContent1 strong').first().positionedOffset()[1]+'px'})

Here’s how it works. PackageMenuContent1 is the inner DIV that you are scrolling with the buttons.

You already have Prototype.js on the page for something else, so we use the Prototype convention $(‘div-id’) to grab that DIV, then apply the setStyle function to it, and pass that function an object with the name of the property we want to set (top) and the value we want to set it to (the positionedOffset#height of the first < strong > tag inside the PackageMenuContent1 DIV).

If you had used a different construction, maybe an unordered list or similar, then there would be a different rule to select it, but the basic principle would stand.

More detail:

The double-dollar $$() selector always returns an array of found objects, even if it finds only one, so the first() function is chained after it to grab only the first instance.

positionedOffset() returns an array, with the left in the first index, and the top in the second. So in this case we get the second item in the array with [1].

This function will return the offset relative to the nearest positioned ancestor, which in this case would be the outer DIV (PackageMenuWindow1), so that gets us the distance we need to scroll down in pixels, but leaves off the pixels units, so we add those on with a simple string concatenation (the + operator).

The reason I’ve gone through all of this in such detail is so that you can fix it up on the other pages. I haven’t looked at any others, and I don’t know if they have the same names for the elements. It’s critical to use the correct names in all of these selectors.

Oh, and to get this to fire when the page loads, you have two basic options. One would be to simply put the script at the bottom of the page. Use Page > HTML Markup, and choose the Before /body slot. Add a script tag and paste this code into it, like this:

<script type="text/javascript">
$('PackageMenuContent1').setStyle({top:-$$('#PackageMenuContent1 strong').first().positionedOffset()[1]+'px'})
</script>

The other would be to use an unobtrusive listener in the head of the page. The following code runs after the page has loaded into the browser, but before anything appears on screen.

<script type="text/javascript">
document.observe('dom:loaded',function(){
    $('PackageMenuContent1').setStyle({top:-$$('#PackageMenuContent1 strong').first().positionedOffset()[1]+'px'})
});
</script>

Let me know how this works out, your site looks really nice, and you’re right – I think this will do that last little bit to make your navigation work.

Walter


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thanks for the help Walter. This is great… except that I can’t get it to work. I pasted the script in Before /body on the Perfect Glue page, but the menu doesn’t budge in preview or my browser. Javascript might as well be Cherokee to me, but I think I understand your explanation: on page load, you’re assigning a style to the inner “scrolled” object with a top offset of -(calculated value)px.

To be honest, I’d be happy if I could just script that menu to scroll up by a specific value (120px or whatever) on load for certain pages, but so far I haven’t been able to move it at all. :frowning:

Thanks for the compliment, too. I just discovered Freeway last month, and this is only my second attempt. I had my first site up in a week, complete with e-commerce and a Flash animation, despite a total lack of programming knowledge. Awesome software!


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

FWIW, based on my [completely naive] interpretation of your code, I tried this, but it didn’t work–


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Close, but you need to wrap the style property in curly-brackets to
make it an object, and put quotes around the value part of the key/
value pair:

 $('PackageMenuContent1').setStyle({top:'-120px'})

Walter

On Nov 14, 2008, at 11:05 AM, Shannon Calvert wrote:

FWIW, based on my [completely naive] interpretation of your code, I
tried this, but it didn’t work–


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Much obliged, but it still doesn’t work. I copied your script into Page>HTML Markup>Before /body, but it doesn’t have any affect that I can see.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

To clarify, I replaced the second line with your code. Nothing happens.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Could you upload this to your server so I can look at it in Firebug?

Walter

On Nov 14, 2008, at 1:08 PM, Shannon Calvert wrote:

Much obliged, but it still doesn’t work. I copied your script into
Page>HTML Markup>Before /body, but it doesn’t have any affect that
I can see.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Done! The script was added to this page: http://www.lwbranddesign.com/PackageStudies/PerfectGlue/index.html


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Okay, there are no errors, so it must be a race condition. This code
runs immediately after the page has loaded, perhaps the initialize
code in your slider control re-sets it to zero later after that.
Let’s try adding a half-second delay to the mix, and see if we can
get this to work:

 Element.setStyle.delay(0.5,'PackageMenuContent1',{top:'-120px'});

Walter

On Nov 14, 2008, at 1:26 PM, Shannon Calvert wrote:

Done! The script was added to this page: http://
www.lwbranddesign.com/PackageStudies/PerfectGlue/index.html


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

BINGO, it works! Now I only have to play around with the numbers to get the right offset. Thanks a million!


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

This is the code I used. It seems redundant, but it works-


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Follow up: it still works with only the first part and a tiny delay-

Thanks again.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

delay() wraps around the first call and makes it wait its turn. There
is no need for the second invocation, you should drop it as it will
make this harder to modify later.

Are all of your pages using the same name for this element? If so,
you could abstract this out so you don’t have to find the right
offset for each one. The browser already knows where the element is.
You just have to ask it.

Element.setStyle.delay(0.05,'PackageMenuContent1',{
	top:'-'
	+ $('PackageMenuContent1').down('strong').positionedOffset()[1]
	+ 'px'
});

As long as the parent element is always called PackageMenuContent1,
and the selected element in the list is always emboldened with
“strong”, this will Just Work™.

Walter

On Nov 14, 2008, at 1:47 PM, Shannon Calvert wrote:

This is the code I used. It seems redundant, but it works-


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Yes, the names are all the same, and your code does indeed cause the menu to scroll to the appropriate item. However by scrolling all the way to the top, the previous page is no longer available, and the last pages on the list look like they’re the only choice in the menu. The manual settings work better for me (although the offset will be incorrect if visitors change he text size).

Now I just have to figure out why the script isn’t working with one group of pages (Rhino Ultra). The item names are all the same, but that group refuses to scroll. I might just rebuild those pages.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options