On 17/02/07, Karl Swedberg <[EMAIL PROTECTED]> wrote:
> On Feb 17, 2007, at 4:36 PM, Joel Birch wrote:
>
> > On 18/02/2007, at 6:13 AM, Karl Swedberg wrote:
> >> ok, now that the smooth scrolling has Klaus's blessing :) ... I
> >> thought I'd share some code I wrote to make it happen for all same-
> >> page links. It takes into account the difference in the way the
> >> various browsers treat href.
> >
> > Thanks for sharing that code Karl.
> > Okay, that has me worried that my solution (which I thought worked
> > perfectly) may be flawed in ways I was not aware of. Here is the code
> > I am using to attach the scrollTo plugin to elements that have a
> > class of "scrolls":
> >
> > (function($){
> >       $.fn.scrollToHash = function(){
> >               return this.each(function(){
> >                       $(this).click(function(){
> >                               
> > $(this.hash).ScrollTo(1500,'easeout')[0].blur();
> >                               return false;
> >                       });
> >               });
> >       };
> > )(jQuery);
> >
> > $(function(){
> >       $("a.scrolls").scrollToHash();
> > });
> >
> > Karl, I'm sure there must be issues I am not aware of here. Can you
> > or anyone point them out for me? This code seems to work in all the
> > browsers I have tested including IE6 and IE7, it's the exact code I
> > am using on Preshil, but maybe I am overlooking something.
> > Thanks.
> >
> > Joel.
>
> Hey Joel,
>
> By using this.hash and limiting it to links with the "scrolls" class,
> you seem to have things covered.
>
> I put my solution together for a content-managed site in which pages
> are being created and updated by people with very limited (okay, non-
> existent) HTML skills. I couldn't expect them to  know what they were
> doing, let alone add a class to the links. Therefore, I had to
> consider the following:
>
> 1. Links to other pages might have the hash as well. Only the ones to
> the same page should use ScrollTo()
> 2. The links could be going to an element with id="something" or to
> an anchor with only name="something"
>
> As Klaus mentioned, I probably could have simplified things a bit by
> using this.hash. But because I had to account for other things that
> the "editors" might do, I had to make the code a bit more complex
> than yours. If I could have made them add a class to their same-page
> links, that would have cut down my code significantly, and it would
> have looked more like yours.
>
> I hope that makes sense. I'm typing this while away from home, so my
> mind isn't totally here. :)
>
>
> --Karl

Are you aware of the pathname and host properties? If you use it, you
can cut down the code further:

$('[EMAIL PROTECTED]"#"]').click(function() {
  if (location.pathname == this.pathname && location.host == this.host) {
    if ($(this.hash).length > 0 ) {
      $(this.hash).ScrollTo(400);
      return false;
    } else {
      $linkDest = $("[EMAIL PROTECTED]" + this.hash.slice(1) +']');
      if ($linkDest.length > 0) {
        $linkDest.ScrollTo(400);
        return false;
      }
    }
  }
});

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to