Skip to content

Commit

Permalink
Added throttling on window resize updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jonom committed Sep 5, 2014
1 parent 271c975 commit ccf8d7e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,34 @@ Usually the best place for this will be inside your `$(document).ready()` functi
$('.focuspoint').focusPoint();
```

There aren't many configuration options available at the moment, but if you want to you can prevent images being re-focused when the window is resized like so:
That's it!

#### Configuration options

By default images are re-focused when the window is resized. You can disable this like so:

```javascript
$('.focuspoint').focusPoint({
reCalcOnWindowResize : false
});
```

You can also change how often images are re-focused during window resizing:

```javascript
//Re-focus images at most once every 100ms
$('.focuspoint').focusPoint({
throttleDuration: 100
});

//Disable throttling
$('.focuspoint').focusPoint({
throttleDuration: 0
});
```

#### Other functions

You can re-focus images at any time with `adjustFocus()`. This recalculates where the image should be positioned in the frame. An example where you may need to do this is if you are using FocusPoint images within a slider. FocusPoint can't do it's calculations properly if an image container is hidden (as it won't have any dimensions), so you should trigger `adjustFocus()` on the target container as soon as it becomes visible. Example:

```javascript
Expand Down Expand Up @@ -111,6 +131,8 @@ If FocusPoint helped you impress a client and you want to say thanks, you're wel

## Changelog

#### v1.0.3 2014-09-06
Throttled window resize updates
#### v1.0.2 2014-09-05
Made setting image width and height on shell optional (thanks @luruke)
#### v1.0.1 2014-09-04
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery-focuspoint",
"version": "1.0.2",
"version": "1.0.3",
"homepage": "https://github.com/jonom/jquery-focuspoint",
"authors": [
"Jonathon Menz"
Expand Down
2 changes: 1 addition & 1 deletion focuspoint.jquery.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"focus",
"focuspoint"
],
"version": "1.0.2",
"version": "1.0.3",
"author": {
"name": "Jonathon Menz",
"url": "http://jonathonmenz.com"
Expand Down
16 changes: 13 additions & 3 deletions js/jquery.focuspoint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* jQuery FocusPoint; version: 1.0.2
* jQuery FocusPoint; version: 1.0.3
* Author: http://jonathonmenz.com
* Source: https://github.com/jonom/jquery-focuspoint
* Copyright (c) 2014 J. Menz; MIT License
Expand All @@ -10,18 +10,28 @@
$.fn.focusPoint = function(options) {
var settings = $.extend({
//These are the defaults.
reCalcOnWindowResize: true
reCalcOnWindowResize: true,
throttleDuration: 17 //ms - set to 0 to disable throttling
}, options);
return this.each(function() {
//Initial adjustments
var container = $(this);
var container = $(this), isThrottled = false;
//Replace basic css positioning with more accurate version
container.removeClass('focus-left-top focus-left-center focus-left-bottom focus-center-top focus-center-center focus-center-bottom focus-right-top focus-right-center focus-right-bottom');
//Focus image in container
container.adjustFocus();
if (settings.reCalcOnWindowResize) {
//Recalculate each time the window is resized
$(window).resize(function() {
//Throttle redraws
if (settings.throttleDuration > 0){
if (isThrottled) { return; }
isThrottled = true;
setTimeout(function () {
isThrottled = false;
container.adjustFocus();
}, settings.throttleDuration);
}
container.adjustFocus();
});
}
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.focuspoint.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ccf8d7e

Please sign in to comment.