Giter VIP home page Giter VIP logo

jquery-smartresize's Introduction

Debounced and Throttled Resize Events for jQuery

It has always been a pain to deal with cross browser issues of the window's resize event:

  • in IE, many resize events fire as long as the user continues resizing the window,
  • Chrome an Safari behave like IE, but resize events always fire two by two,
  • Firefox used to fire one resize event at the end of the resizing, but now behaves like IE,
  • Opera behaves like IE, but fires resize events at a reduced rate.

This project offers two scripts, each providing a special jQuery event that make resize more manageable:

  • jquery.debouncedresize.js: adds a special event that fires once after the window has been resized,
  • jquery.throttledresize.js: adds a special event that fires at a reduced rate (no more double events from Chrome and Safari).

The Demo should help you make your choice.

Note to previous users: jquery.debouncedresize.js is the equivalent of the old jquery.smartresize.js, only the name of the special event changes. Update is not required unless you want to add jquery.throttledresize.js to a page page that already has jquery.smartresize.js.

Binding / Unbinding

Simply bind your special event just like a normal resize event.

$(window).on("debouncedresize", function( event ) {
	// Your event handler code goes here.
});

// or...
$(window).on("throttledresize", function( event ) {
	// Your event handler code goes here.
});

// unbind at will
$(window).off( "debouncedresize" );

Threshold

Both special events have a .threshold option:

  • in jquery.debouncedresize.js, it defines the interval used to determine if two resize events are part of the same debouncedresize event. Defaults to 150 (milliseconds)
  • in jquery.throttledresize.js, it defines the number of animation ticks (or frames) between each throttledresize event. Defaults to 0 (tick), which means that it's going to fire at a maximum of 60fps.

They can be modified globally once the script has been loaded:

// increase the threshold to 250ms
$.event.special.debouncedresize.threshold = 250;

// decrease the firing rate to a maximum of 30fps
$.event.special.throttledresize.threshold = 1;
// 2 <=> 20fps, 3 <=> 15fps, ...

(Synchronous) Trigger

Triggering those events is achieved using jQuery's standard API:

$(window).trigger( "debouncedresize" );

It's also possible to execute the handler of any listener synchronously (without the delays):

$(window).trigger( "throttledresize", [true] );

Minimalist Standalone Version

Most of the time, I find myself using debouncedresize just to register a single listener on window. As it turns out, all the features I need actually fit in 91 bytes:

// debulked onresize handler
function on_resize(c,t){onresize=function(){clearTimeout(t);t=setTimeout(c,100)};return c};

Using it is pretty simple:

on_resize(function() {
  // handle the resize event here
  ...
});

Initializing a page (by executing the resize handler when the page loads) couldn't be easier:

on_resize(function() {
  ...
})(); // these parenthesis does the trick

No files are provided for this function, simply copy/paste it from this README.

License

MIT licensed http://louisremi.mit-license.org/

Copyright (c) 2012 Louis-Rémi Babé.

jquery-smartresize's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery-smartresize's Issues

jquery 1.8 support

First, let me just say thanks for this library. It's awesome.

It seems to be broken for me with jQuery 1.8. I only get a single throttledresize event instead of the expected 60fps. It works perfectly with 1.7.2.

There are no errors on the console. I just don't get the throttledresize event.

Dynamically changing visit website button

Hello your plug in is quite nice! I am using it in a project and I would like to make the visit website button appear only when there is an actual link on the href attribute. I have been trying but so far I can only make go away everything else but the "visit website button"

`
if(eldata.href == "#"){
//HERE IS WHERE THERE IS NO LINK AND WHERE I CANT REMOVE DE BUTTON
console.log("sin definir link");
this.$title.html( eldata.title );
this.$description.html( eldata.description );
this.$href.attr( 'href', eldata.href );
}else if(eldata.href != "#"){
console.log("LINK");
this.$title.html( eldata.title );
this.$description.html( eldata.description );
this.$href.attr( 'href', eldata.href );
}


Thanks before hand !

Occasional resize bug

Hey there (continuing from Twitter),

This code has been brilliant! Nice.

I'm not much of a coder myself, but have used this script as part of a jQuery Accordion script (http://tympanus.net/Tutorials/FlexibleSlideToTopAccordion/). It was working perfectly at first, now I have this bug.

This is the site I used it on: http://www.elementalchile.cl/j_rdsn/

What happens is, when I click a tab (any tab at random), about 1/5 of the time, it only half opens. I've attached a screen grab below to show better. In the HTML/CSS, the height coming through is calculated wrong on the "st-open" class - the height is 133px. In order to recreate this error constantly, it's possible to resize the page width, and try another tab. It seems random as to when it'll happen, and can happen to any tag :-/

It would be massively appreciated if you can see why it is. Once we've fixed this, I can launch it!

Thanks very much for your time - its a big help

Jamie

screen shot 2015-05-28 at 14 59 57

Event fires on scroll in iOS 8

This event will fire on scroll when running iOS 8 and Safari. Does not fire in Chrome with iOS 8. Tested on iPad, iPad mini, iPhone.

[enhancement] Add missing bower.json.

Hey, maintainer(s) of louisremi/jquery-smartresize!

We at VersionEye are working hard to keep up the quality of the bower's registry.

We just finished our initial analysis of the quality of the Bower.io registry:

7530 - registered packages, 224 of them doesnt exists anymore;

We analysed 7306 existing packages and 1070 of them don't have bower.json on the master branch ( that's where a Bower client pulls a data ).

Sadly, your library louisremi/jquery-smartresize is one of them.

Can you spare 15 minutes to help us to make Bower better?

Just add a new file bower.json and change attributes.

{
  "name": "louisremi/jquery-smartresize",
  "version": "1.0.0",
  "main": "path/to/main.css",
  "description": "please add it",
  "license": "Eclipse",
  "ignore": [
    ".jshintrc",
    "**/*.txt"
  ],
  "dependencies": {
    "<dependency_name>": "<semantic_version>",
    "<dependency_name>": "<Local_folder>",
    "<dependency_name>": "<package>"
  },
  "devDependencies": {
    "<test-framework-name>": "<version>"
  }
}

Read more about bower.json on the official spefication and nodejs semver library has great examples of proper versioning.

NB! Please validate your bower.json with jsonlint before commiting your updates.

Thank you!

Timo,
twitter: @versioneye
email: [email protected]
VersionEye - no more legacy software!

Infinite interval in jquery.throttledresize.js

Hello, just found out, that the setInterval is called once in jquery.throttledresize.js and clearInterval is never called. So the function is run every 30ms infinitely.
Before it was
$(dummy).animate(
...
$(dummy).stop();
...
);
and now the animate was changed into setInterval, so $(dummy).stop(); should be changed into clearInterval.

Multiple instances

I'm having trouble using the minimal standalone version with multiple instances. I'm no JS expert, so I'm probably missing something obvious. But if I do the following, all that gets logged is "second". How can I get both to work without having them within the same function call?

function on_resize(c,t){onresize=function(){clearTimeout(t);t=setTimeout(c,300)};return c};

on_resize(function(){
    log('first');
});
on_resize(function(){
    log('second');
});

this saves one byte :)

function on_resize(a,b){return onresize=function(){clearTimeout(b),b=setTimeout(a,100)},a}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.