views:

40

answers:

1

When I jump to an anchor tag in on a wide (4000px) page, the anchored image is horizontally aligned to the right. How can I get it to align to the center? I have tried several things but none seem to work. Since I am new here I am not allowed to post the code, so I hope I was clear enough.

Thanks for your help,

Robert C.

A: 

The issue arises because the browser will do the least amount of scrolling required to bring the anchored element into view. Thus, if it is off to the right of the viewable area, when the anchor tag is clicked, it will simply scroll far enough to the right to reveal the whole element.

You could fix this behavior with javascript if that is an option. You won't be able to center it on #anchor click with straight CSS.

Here is a "solution" using jQuery. I have "solution" in quotes because it may introduce more problems than you want. For instance, without further JS the back button won't work to go to the previous link. But this does center the item given your sample code.

Just add this to the page you linked above before the closing <head> tag:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;  
<script type="text/javascript">
$(function(){
   $("a[href^=#]").click(function(e){
    var id      = $(this).attr('href').substr(1),
        $target = $('*[name=' + id + '] img'),
        $window = $(window),
        offset  = $target.offset();

    var left = offset.left - ($window.width() / 2) + ($target.width() / 2 );
    var top  = offset.top  - ($window.height() / 2) + ($target.height() / 2);


    $('html,body').scrollTop(top).scrollLeft( left);

       e.preventDefault(); // Keep the browser from trying
   }); 
});
</script>

It will find all a tags with internal links (href starts with #). Then it finds the target that has that name, and grabs the child img tag. Then it gets the dimensions of the img element and the window element. Does some math with the img elements offset, and scrolls it into center.

Doug Neiner
Thank you for the fast reply. Is the JS solution one that I could find online? I am new to this so writing the javascript might be out of my capabilities for the time being.
Robert C.
Hey @Robert, I think I can help, but 2 questions first. Can you post a link of what you have so far? And #2 are you opposed to using jQuery (56k) on the page?
Doug Neiner
Doh... didn't see the link above, ok so just answer question #2 :)
Doug Neiner
I am definitely not opposed to jQuery, the size will not be an issue, the trouble is, I am basically a beginner and the learning curve might be a bit steep.
Robert C.
Wow, dcneiner, thank you so much! Above and beyond the call of duty. This is perfect because I will not need the back button for this project. I have to get to bed now but I can't wait to study and try the code. Thanks again. I will be sure to pass on the favor once I get some knowledge worth sharing.
Robert C.
This may be one of problems you mentioned, the code works great in Firefox but in Safari the images are centered horizontally but aligned through the center of the image with the top of the browser window. Is there a fix for this? Thank you so much for your help.
Robert C.
....oh, and an example is at cslack.com/test.html.
Robert C.
Hey @Robert, what version of Safari are you using? It is working in Safari 4 for me as expected. I have noticed with Safari 4 that some websites don't fill the browser (backgroundcolor) until the browser is resized. Does it work after you resize the browser?
Doug Neiner
I had to upgrade to Safari 4. It works! Thanks again for all of your help.
Robert C.
No problem, if this answer is correct, be sure to mark it as solved. And good luck with your project!
Doug Neiner