Skip to content

Commit

Permalink
v1.3.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
tgomilar committed Jan 12, 2018
1 parent 099e185 commit 125656d
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 197 deletions.
47 changes: 27 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

[![npm](https://img.shields.io/npm/v/paroller.js.svg)](https://www.npmjs.com/package/paroller.js)

A lightweight jQuery plugin that enables parallax scrolling effect.
A lightweight jQuery plugin that enables parallax scrolling effect
- You can use it on elements with background property or on any other element
- While scrolling elements can move: vertical, horizontal
- Manipulated through *html data-** attributes
- Manipulated through html data-* attributes or jQuery options
- Mobile ready
- Easy to use

Expand All @@ -22,6 +22,7 @@ Before closing ```</body>``` element include:
```sh
$ npm install paroller.js
```

#### Bower
```sh
$ bower install paroller.js
Expand All @@ -30,41 +31,47 @@ $ bower install paroller.js
```sh
$ yarn add paroller.js
```
#### Run
#### Use
```javascript
// initialize paroller.js
$("body").paroller();
// initialize paroller.js
$('.my-paroller').paroller();
// initialize paroller.js and set attributes
$("#my-element").paroller({ factor: '0.5', type: 'foreground', direction: 'horizontal' });
```
```html
<!-- select element -->
<div data-paroller-factor="0.3"></div>
<div class="my-paroller" data-paroller-factor="0.3" data-paroller-type="foreground" data-paroller-direction="horizontal"></div>
<div id="my-element"></div>
```

## Usage
### data attribute
To enable Paroller on element you have to add *data-paroller-factor* to selected element.
#####npm and browserify
```sh
require('paroller.js');
```

## Options
### data attributes
You can control scrolling parallax effect by setting data-paroller-* values attributes or by setting JavaScript options.

*data-paroller-factor* sets speed and distance of element's parallax effect on scroll.


| data-* | value | default value |
| ------ | ------ | ------ |
| data-paroller-factor | invokes *jquery.paroller.js* functionality. It sets elements offset and speed. It can be positive (0.3) or negative (-0.3). Less means slower. | 0 |
| data-paroller-factor | sets offset and speed. It can be positive (0.3) or negative (-0.3). Less means slower. | 0 |
| data-paroller-type | background, foreground | background |
| data-paroller-direction | vertical, horizontal | vertical |

### JavaScript
```javascript
// initialize paroller.js
$(window).paroller();
// initialize paroller.js and set attributes for selected elements
$(".paroller, [data-paroller-factor]").paroller({
factor: 0.3, // multiplier for scrolling speed and offset, +- values for direction control
type: 'foreground', // background, foreground
direction: 'horizontal' // vertical, horizontal
});

```
```javascript
// initialize paroller.js and set attributes for #element
$("#element").paroller({ factor: '0.5', type: 'foreground', direction: 'horizontal' });
```

### Todos

- direction: diagonal

License
----
Expand Down
6 changes: 2 additions & 4 deletions assets/style.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
html {
overflow-x: hidden;

}

.logo-arrow {
position: absolute;
left: 20px;
Expand Down Expand Up @@ -31,15 +29,15 @@ html {
height: 25px;
background: #222;
top: 0;
left: 15px;
left: 15.5px;
animation: changePosition 2s linear 2s infinite alternate;
}
nav {
border-bottom: 1px solid rgba(0,0,0,.08);
}
#vertical,
#horizontal {
padding: 200px 0;
padding: 50px 0;
scroll-behavior: smooth;
}
#vertical .jumbotron {
Expand Down
210 changes: 107 additions & 103 deletions dist/jquery.paroller.js
Original file line number Diff line number Diff line change
@@ -1,119 +1,123 @@
/**
* jQuery plugin paroller.js v1.0
* jQuery plugin paroller.js v1.3.0
* https://github.com/tgomilar/paroller.js
* preview: https://tgomilar.github.io/paroller/
**/
(function (factory) {
'use strict';
(function (factory) {
'use strict';

if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = factory(require('jquery'));
}
else {
factory(jQuery);
}
})(function ($) {
'use strict';
if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = factory(require('jquery'));
}
else {
factory(jQuery);
}
})(function ($) {
'use strict';

var setDirection = {
bgVertical: function (elem, bgOffset) {
return elem.css({'background-position': 'center ' + -bgOffset + 'px'});
},
bgHorizontal: function (elem, bgOffset) {
return elem.css({'background-position': -bgOffset + 'px' + ' center'});
},
vertical: function (elem, elemOffset) {
return elem.css({
'-webkit-transform': 'translateY(' + elemOffset + 'px)',
'-moz-transform': 'translateY(' + elemOffset + 'px)',
'transform': 'translateY(' + elemOffset + 'px)'
});
},
horizontal: function (elem, elemOffset) {
return elem.css({
'-webkit-transform': 'translateX(' + elemOffset + 'px)',
'-moz-transform': 'translateX(' + elemOffset + 'px)',
'transform': 'translateX(' + elemOffset + 'px)'
});
}
};
var setDirection = {
bgVertical: function (elem, bgOffset) {
return elem.css({'background-position': 'center ' + -bgOffset + 'px'});
},
bgHorizontal: function (elem, bgOffset) {
return elem.css({'background-position': -bgOffset + 'px' + ' center'});
},
vertical: function (elem, elemOffset) {
return elem.css({
'-webkit-transform': 'translateY(' + elemOffset + 'px)',
'-moz-transform': 'translateY(' + elemOffset + 'px)',
'transform': 'translateY(' + elemOffset + 'px)',
'transition': 'transform linear',
'will-change': 'transform'
});
},
horizontal: function (elem, elemOffset) {
return elem.css({
'-webkit-transform': 'translateX(' + elemOffset + 'px)',
'-moz-transform': 'translateX(' + elemOffset + 'px)',
'transform': 'translateX(' + elemOffset + 'px)',
'transition': 'transform linear',
'will-change': 'transform'
});
}
};

$.fn.paroller = function (options) {
var windowHeight = $(window).height();
var documentHeight = $(document).height();
$.fn.paroller = function (options) {
var windowHeight = $(window).height();
var documentHeight = $(document).height();

// default options
var options = $.extend({
factor: 0, // - to +
type: 'background', // foreground
direction: 'vertical' // horizontal
}, options);
// default options
var options = $.extend({
factor: 0, // - to +
type: 'background', // foreground
direction: 'vertical' // horizontal
}, options);

return this.each(function () {
var working = false;
var $this = $(this);
var offset = $this.offset().top;
var height = $this.outerHeight();
var dataFactor = $this.data('paroller-factor');
var dataType = $this.data('paroller-type');
var dataDirection = $this.data('paroller-direction');
return this.each(function () {
var working = false;
var $this = $(this);
var offset = $this.offset().top;
var height = $this.outerHeight();
var dataFactor = $this.data('paroller-factor');
var dataType = $this.data('paroller-type');
var dataDirection = $this.data('paroller-direction');

var factor = (dataFactor) ? dataFactor : options.factor;
var type = (dataType) ? dataType : options.type;
var direction = (dataDirection) ? dataDirection : options.direction;
var bgOffset = Math.round(offset * factor);
var transform = Math.round((offset - (windowHeight / 2) + height) * factor);
var factor = (dataFactor) ? dataFactor : options.factor;
var type = (dataType) ? dataType : options.type;
var direction = (dataDirection) ? dataDirection : options.direction;
var bgOffset = Math.round(offset * factor);
var transform = Math.round((offset - (windowHeight / 2) + height) * factor);

if (type == 'background') {
if (direction == 'vertical') {
setDirection.bgVertical($this, bgOffset);
}
else if (direction == 'horizontal') {
setDirection.bgHorizontal($this, bgOffset);
}
}
else if (type == 'foreground') {
if (direction == 'vertical') {
setDirection.vertical($this, transform);
}
else if (direction == 'horizontal') {
setDirection.horizontal($this, transform);
}
}
if (type == 'background') {
if (direction == 'vertical') {
setDirection.bgVertical($this, bgOffset);
}
else if (direction == 'horizontal') {
setDirection.bgHorizontal($this, bgOffset);
}
}
else if (type == 'foreground') {
if (direction == 'vertical') {
setDirection.vertical($this, transform);
}
else if (direction == 'horizontal') {
setDirection.horizontal($this, transform);
}
}

var scrollAction = function () {
working = false;
};
var scrollAction = function () {
working = false;
};

$(window).on('scroll', function () {
if (!working) {
var scrolling = $(this).scrollTop();
$(window).on('scroll', function () {
if (!working) {
var scrolling = $(this).scrollTop();
documentHeight = $(document).height();

bgOffset = Math.round((offset - scrolling) * factor);
transform = Math.round(((offset - (windowHeight / 2) + height) - scrolling) * factor);
bgOffset = Math.round((offset - scrolling) * factor);
transform = Math.round(((offset - (windowHeight / 2) + height) - scrolling) * factor);

if (type == 'background') {
if (direction == 'vertical') {
setDirection.bgVertical($this, bgOffset);
}
else if (direction == 'horizontal') {
setDirection.bgHorizontal($this, bgOffset);
}
}
else if ((type == 'foreground') && (scrolling < documentHeight)) {
if (direction == 'vertical') {
setDirection.vertical($this, transform);
}
else if (direction == 'horizontal') {
setDirection.horizontal($this, transform);
}
}
if (type == 'background') {
if (direction == 'vertical') {
setDirection.bgVertical($this, bgOffset);
}
else if (direction == 'horizontal') {
setDirection.bgHorizontal($this, bgOffset);
}
}
else if ((type == 'foreground') && (scrolling <= documentHeight)) {
if (direction == 'vertical') {
setDirection.vertical($this, transform);
}
else if (direction == 'horizontal') {
setDirection.horizontal($this, transform);
}
}

window.requestAnimationFrame(scrollAction);

working = true;
}
}).trigger('scroll');
});
};
});
window.requestAnimationFrame(scrollAction);
working = true;
}
}).trigger('scroll');
});
};
});
2 changes: 1 addition & 1 deletion dist/jquery.paroller.min.js

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

Loading

0 comments on commit 125656d

Please sign in to comment.