DISQUS

DISQUS Hello! ProDevTips is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

ProDevTips

Mainly tutorials and tips relating to web development with PHP, Javascript, jQuery and Ruby
Jump to original thread »
Author

jQuery - making scrolling and toggling simple

Started by Henrik · 10 months ago

I recently discovered jQuery and I thought it looked great. I could hardly wait to try it out in a project and the time has finally come.
In this case we have a list of feeds with the following requirements:
1.) When a link next to the title of each item is pressed we want to display [...% ... Continue reading »

11 comments

  • Hi, doesn't this work as expected ?

    function toggleViz(el_id){
    $("#"+el_id).toggleClass("hidden_simple");
    if($("#"+el_id).attr("className") != "hidden_simple"){
    // see the modification here V
    $.scrollTo( "#"+el_id, {speed:500});
    }
    }
  • The <a href="javascript:toggleViz('{$feed.id}')" rel="nofollow">more stuff could probably be implemented with some kind of callback event functionality. It's on my to do list.
  • Awesome Ariel, I didn't see that one in your example (didn't look very hard though). So I suppose we can throw out the dimension plugin in this example.
  • Indeed.. since the... third release, more or less. The plugin stopped relying on dimensions.
    If you are interested, that function could be shorter written like this:

    function toggleViz(el_id){
    var $el = $("#"+el_id).toggleClass("hidden_simple");
    if ( !$el.is('.hidden_simple') )
    $.scrollTo( $el, {speed:500});
    };

    I do admit the examples don't show all the possible uses of the plugin. But the source code has some more examples and explains each customizable option too.
    Please let me know, if you think it's not clear enough, thanks.
  • I think the problem was that I looked to dimensions before I found scrollto ,therefore I knew I could get the position of an element with dimensions and that made me not review your stuff like I should have it seems. I think everything is clear enough, thanks for an awesome plugin!
  • If you're using jQuery 1.2.1, you can use the hasClass() method instead of is() so you could re-write part of Ariel's code from:

    !$el.is(’.hidden_simple')

    to

    !$el.hasClass(’.hidden_simple')

    I ran a few very basic speed tests and found the hasClass() method to be about 30% faster.
  • Can you post a demo of effect you are doing?

    Thanks,
    Rich
  • Thanks Dave!

    Rich: I will link to that whole project when it is finished. In the meantime what we do here is simply making the main browser window scroll to make the element clicked centered vertically. The purpose is to enhance the usability of the interface.
  • @dave
    If you use hasClass, you must remove the dot leading the className. As for speed.. that is odd, because if you take a look at hasClass:

    hasClass: function(expr) {
    return this.is("." + expr);
    },

    It's doing, internally, the same as I did. I don't think it can be really faster. hasClass is useful when you have the className in a variable, so it saves you the '#' + klass .
  • Oops.. I mean '.' + klass. Not '#' + klass.
  • How about a demo about 'load on scroll' or 'pageless scrolling'

    example at www.dzone.com and just try to scroll down on the page. pretty neat!

Add New Comment

Returning? Login