Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
960468fc9e | ||
|
|
42d528a839 | ||
|
|
26e9ff1cac | ||
|
|
38b8b818e8 | ||
|
|
938f3134d7 | ||
|
|
aa213398f3 | ||
|
|
1cdbc9509f | ||
|
|
627b6a6b81 | ||
|
|
0099bb556c | ||
|
|
2e1e9f2f75 | ||
|
|
f3b6fd520c | ||
|
|
a6bb9d02a3 | ||
|
|
639f369d33 | ||
|
|
dacb601f9f | ||
|
|
0c9dc8dbb4 | ||
|
|
f32e32485b | ||
|
|
a4bee31597 |
31
README.md
31
README.md
@@ -39,12 +39,26 @@ It's cool, isn't it?
|
||||
Requirements
|
||||
------------
|
||||
|
||||
To make this plugin *perfect*, some requirements was not avoidable. But they're all very trivial and there's nothing to worry.
|
||||
To make this plugin *perfect*, some requirements were not avoidable. But they're all very trivial and there's nothing to worry about.
|
||||
|
||||
* there must be the *one* content element(like div) for the container.
|
||||
* the container must have a 'position' css style.
|
||||
* the scrollbar's position must be 'absolute'.
|
||||
* the scrollbar-x must have a 'bottom' css style, and the scrollbar-y must have a 'right' css style.
|
||||
|
||||
Optional parameters
|
||||
-------------------
|
||||
|
||||
perfect-scrollbar supports optional parameters.
|
||||
|
||||
### wheelSpeed
|
||||
The scroll speed applied to mousewheel event.
|
||||
Default: 10
|
||||
|
||||
### wheelPropagation
|
||||
If this option is true, when the scroll reach the end of the side, mousewheel event will be propagated to parent element.
|
||||
Default: false
|
||||
|
||||
|
||||
How to Use
|
||||
----------
|
||||
@@ -63,6 +77,15 @@ When the html document is like above, just use like this:
|
||||
```javascript
|
||||
$('#Demo').perfectScrollbar();
|
||||
```
|
||||
|
||||
With optional parameters:
|
||||
```javascript
|
||||
$("#Demo").perfectScrollbar({
|
||||
wheelSpeed: 20,
|
||||
wheelPropagation: true
|
||||
})
|
||||
```
|
||||
|
||||
If the size of your container or content changes:
|
||||
```javascript
|
||||
$('#Demo').perfectScrollbar('update');
|
||||
@@ -72,6 +95,12 @@ If you want to destory the scrollbar:
|
||||
$('#Demo').perfectScrollbar('destroy');
|
||||
```
|
||||
|
||||
If you want to scroll to somewhere, just use scroll-top css and update.
|
||||
```javascript
|
||||
$("#Demo").scrollTop(0);
|
||||
$("#Demo").perfectScrollbar('update');
|
||||
```
|
||||
|
||||
Very helpful friends
|
||||
--------------------
|
||||
|
||||
|
||||
3
build
3
build
@@ -1,3 +1,4 @@
|
||||
#!/bin/sh
|
||||
uglifyjs src/perfect-scrollbar.js > min/perfect-scrollbar.min.js
|
||||
uglifyjs src/perfect-scrollbar.js -o min/perfect-scrollbar.min.js
|
||||
uglifyjs src/perfect-scrollbar.js src/jquery.mousewheel.js -o min/perfect-scrollbar.with-mousewheel.min.js
|
||||
cleancss src/perfect-scrollbar.css -o min/perfect-scrollbar.min.css
|
||||
|
||||
BIN
examples/azusa.jpg
Normal file
BIN
examples/azusa.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 128 KiB |
29
examples/options-default.html
Normal file
29
examples/options-default.html
Normal file
@@ -0,0 +1,29 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>perfect-scrollbar example</title>
|
||||
<link href="../src/perfect-scrollbar.css" rel="stylesheet">
|
||||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
|
||||
<script src="../src/jquery.mousewheel.js"></script>
|
||||
<script src="../src/perfect-scrollbar.js"></script>
|
||||
<style>
|
||||
.contentHolder { position:relative; margin:0px auto; padding:0px; width: 600px; height: 400px; overflow: hidden; }
|
||||
.contentHolder .content { background-image: url('./azusa.jpg'); width: 1280px; height: 720px; }
|
||||
.spacer { text-align:center }
|
||||
</style>
|
||||
<script>
|
||||
jQuery(document).ready(function ($) {
|
||||
"use strict";
|
||||
$('#Default').perfectScrollbar();
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="Default" class="contentHolder">
|
||||
<div class="content">
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
40
examples/options-wheelPropagation.html
Normal file
40
examples/options-wheelPropagation.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>perfect-scrollbar example - use wheelPropagation to control propagation of scrolling at extremities</title>
|
||||
<link href="../src/perfect-scrollbar.css" rel="stylesheet">
|
||||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
|
||||
<script src="../src/jquery.mousewheel.js"></script>
|
||||
<script src="../src/perfect-scrollbar.js"></script>
|
||||
<style>
|
||||
.contentHolder { position:relative; margin:0px auto; padding:0px; width: 600px; height: 400px; overflow: hidden; }
|
||||
.contentHolder .content { background-image: url('./azusa.jpg'); width: 1280px; height: 720px; }
|
||||
.spacer { text-align:center }
|
||||
</style>
|
||||
<script>
|
||||
jQuery(document).ready(function ($) {
|
||||
"use strict";
|
||||
$('#Default').perfectScrollbar();
|
||||
$('#NoWheelPropagation').perfectScrollbar({wheelPropagation:true});
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1 style="text-align:center">Default; wheelPropagation:false</h1>
|
||||
<div id="Default" class="contentHolder">
|
||||
<div class="content">
|
||||
</div>
|
||||
</div>
|
||||
<h1 style="text-align:center">wheelPropagation:true</h1>
|
||||
<div id="NoWheelPropagation" class="contentHolder">
|
||||
<div class="content">
|
||||
</div>
|
||||
</div>
|
||||
<div class="spacer">
|
||||
Note<br>that<br>there<br>is<br>plenty<br>of<br>text<br>after<br>the<br>image<br>to<br>ensure<br>that<br>it<br>is<br>possible<br>to<br>down<br>
|
||||
after<br>the<br>bottom<br>of<br>the<br>image<br>has<br>been<br>reached<br>to<br>enable<br>the<br>wheel<br>propagation<br>to<br>be<br>tested<br>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
42
examples/options-wheelSpeed.html
Normal file
42
examples/options-wheelSpeed.html
Normal file
@@ -0,0 +1,42 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>perfect-scrollbar example - use wheelSpeed to change speed of scrolling</title>
|
||||
<link href="../src/perfect-scrollbar.css" rel="stylesheet">
|
||||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
|
||||
<script src="../src/jquery.mousewheel.js"></script>
|
||||
<script src="../src/perfect-scrollbar.js"></script>
|
||||
<style>
|
||||
.contentHolder { position:relative; margin:0px auto; padding:0px; width: 600px; height: 400px; overflow: hidden; }
|
||||
.contentHolder .content { background-image: url('./azusa.jpg'); width: 1280px; height: 720px; }
|
||||
.spacer { text-align:center }
|
||||
</style>
|
||||
<script>
|
||||
jQuery(document).ready(function ($) {
|
||||
"use strict";
|
||||
$('#Default').perfectScrollbar();
|
||||
$('#FastWheelSpeed').perfectScrollbar({wheelSpeed:100});
|
||||
$('#SlowWheelSpeed').perfectScrollbar({wheelSpeed:1});
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1 style="text-align:center">Default; wheelSpeed:10</h1>
|
||||
<div id="Default" class="contentHolder">
|
||||
<div class="content">
|
||||
</div>
|
||||
</div>
|
||||
<h1 style="text-align:center">Fast: wheelSpeed:100</h1>
|
||||
<div id="FastWheelSpeed" class="contentHolder">
|
||||
<div class="content">
|
||||
</div>
|
||||
</div>
|
||||
<h1 style="text-align:center">Slow: wheelSpeed:1</h1>
|
||||
<div id="SlowWheelSpeed" class="contentHolder">
|
||||
<div class="content">
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
4
min/perfect-scrollbar.min.js
vendored
4
min/perfect-scrollbar.min.js
vendored
File diff suppressed because one or more lines are too long
1
min/perfect-scrollbar.with-mousewheel.min.js
vendored
Normal file
1
min/perfect-scrollbar.with-mousewheel.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@@ -2,7 +2,7 @@
|
||||
"name": "perfect-scrollbar",
|
||||
"title": "perfect-scrollbar",
|
||||
"description": "Tiny but perfect jquery scrollbar plugin.",
|
||||
"version": "0.2.4",
|
||||
"version": "0.3.2",
|
||||
"author": {
|
||||
"name": "HyeonJe Jun",
|
||||
"email": "noraesae@yuiazu.net",
|
||||
|
||||
84
src/jquery.mousewheel.js
Normal file
84
src/jquery.mousewheel.js
Normal file
@@ -0,0 +1,84 @@
|
||||
/*! Copyright (c) 2011 Brandon Aaron (http://brandonaaron.net)
|
||||
* Licensed under the MIT License (LICENSE.txt).
|
||||
*
|
||||
* Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
|
||||
* Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
|
||||
* Thanks to: Seamus Leahy for adding deltaX and deltaY
|
||||
*
|
||||
* Version: 3.0.6
|
||||
*
|
||||
* Requires: 1.2.2+
|
||||
*/
|
||||
|
||||
(function($) {
|
||||
|
||||
var types = ['DOMMouseScroll', 'mousewheel'];
|
||||
|
||||
if ($.event.fixHooks) {
|
||||
for ( var i=types.length; i; ) {
|
||||
$.event.fixHooks[ types[--i] ] = $.event.mouseHooks;
|
||||
}
|
||||
}
|
||||
|
||||
$.event.special.mousewheel = {
|
||||
setup: function() {
|
||||
if ( this.addEventListener ) {
|
||||
for ( var i=types.length; i; ) {
|
||||
this.addEventListener( types[--i], handler, false );
|
||||
}
|
||||
} else {
|
||||
this.onmousewheel = handler;
|
||||
}
|
||||
},
|
||||
|
||||
teardown: function() {
|
||||
if ( this.removeEventListener ) {
|
||||
for ( var i=types.length; i; ) {
|
||||
this.removeEventListener( types[--i], handler, false );
|
||||
}
|
||||
} else {
|
||||
this.onmousewheel = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
$.fn.extend({
|
||||
mousewheel: function(fn) {
|
||||
return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
|
||||
},
|
||||
|
||||
unmousewheel: function(fn) {
|
||||
return this.unbind("mousewheel", fn);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
function handler(event) {
|
||||
var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
|
||||
event = $.event.fix(orgEvent);
|
||||
event.type = "mousewheel";
|
||||
|
||||
// Old school scrollwheel delta
|
||||
if ( orgEvent.wheelDelta ) { delta = orgEvent.wheelDelta/120; }
|
||||
if ( orgEvent.detail ) { delta = -orgEvent.detail/3; }
|
||||
|
||||
// New school multidimensional scroll (touchpads) deltas
|
||||
deltaY = delta;
|
||||
|
||||
// Gecko
|
||||
if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
|
||||
deltaY = 0;
|
||||
deltaX = -1*delta;
|
||||
}
|
||||
|
||||
// Webkit
|
||||
if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
|
||||
if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }
|
||||
|
||||
// Add event and delta to the front of the arguments
|
||||
args.unshift(event, delta, deltaX, deltaY);
|
||||
|
||||
return ($.event.dispatch || $.event.handle).apply(this, args);
|
||||
}
|
||||
|
||||
})(jQuery);
|
||||
@@ -2,7 +2,25 @@
|
||||
* Licensed under the MIT License
|
||||
*/
|
||||
((function($) {
|
||||
$.fn.perfectScrollbar = function(option) {
|
||||
|
||||
// The default settings for the plugin
|
||||
var defaultSettings = {
|
||||
wheelSpeed: 10,
|
||||
wheelPropagation: false
|
||||
};
|
||||
|
||||
$.fn.perfectScrollbar = function(suppliedSettings, option) {
|
||||
|
||||
// Use the default settings
|
||||
var settings = $.extend( true, {}, defaultSettings );
|
||||
if (typeof suppliedSettings === "object") {
|
||||
// But over-ride any supplied
|
||||
$.extend( true, settings, suppliedSettings );
|
||||
} else {
|
||||
// If no settings were supplied, then the first param must be the option
|
||||
option = suppliedSettings;
|
||||
}
|
||||
|
||||
if(option === 'update') {
|
||||
if($(this).data('perfect_scrollbar_update')) {
|
||||
$(this).data('perfect_scrollbar_update')();
|
||||
@@ -51,8 +69,8 @@
|
||||
var updateBarSizeAndPosition = function() {
|
||||
container_width = $this.width();
|
||||
container_height = $this.height();
|
||||
content_width = $content.width();
|
||||
content_height = $content.height();
|
||||
content_width = $content.outerWidth(false);
|
||||
content_height = $content.outerHeight(false);
|
||||
if(container_width < content_width) {
|
||||
scrollbar_x_width = parseInt(container_width * container_width / content_width);
|
||||
scrollbar_x_left = parseInt($this.scrollLeft() * container_width / content_width);
|
||||
@@ -60,6 +78,7 @@
|
||||
else {
|
||||
scrollbar_x_width = 0;
|
||||
scrollbar_x_left = 0;
|
||||
$this.scrollLeft(0);
|
||||
}
|
||||
if(container_height < content_height) {
|
||||
scrollbar_y_height = parseInt(container_height * container_height / content_height);
|
||||
@@ -68,6 +87,7 @@
|
||||
else {
|
||||
scrollbar_y_height = 0;
|
||||
scrollbar_y_left = 0;
|
||||
$this.scrollTop(0);
|
||||
}
|
||||
|
||||
$scrollbar_x.css({left: scrollbar_x_left + $this.scrollLeft(), bottom: scrollbar_x_bottom - $this.scrollTop(), width: scrollbar_x_width});
|
||||
@@ -88,7 +108,7 @@
|
||||
scrollbar_x_left = new_left;
|
||||
}
|
||||
$scrollbar_x.css({left: scrollbar_x_left + $this.scrollLeft()});
|
||||
}
|
||||
};
|
||||
|
||||
var moveBarY = function(current_top, delta_y) {
|
||||
var new_top = current_top + delta_y,
|
||||
@@ -164,23 +184,107 @@
|
||||
|
||||
// bind handlers
|
||||
var bindMouseWheelHandler = function() {
|
||||
var shouldPreventDefault = function(deltaX, deltaY) {
|
||||
var scrollTop = $this.scrollTop();
|
||||
if(scrollTop == 0 && deltaY > 0 && deltaX == 0) {
|
||||
return !settings["wheelPropagation"];
|
||||
}
|
||||
else if(scrollTop >= content_height - container_height && deltaY < 0 && deltaX == 0) {
|
||||
return !settings["wheelPropagation"];
|
||||
}
|
||||
|
||||
var scrollLeft = $this.scrollLeft();
|
||||
if(scrollLeft == 0 && deltaX < 0 && deltaY == 0) {
|
||||
return !settings["wheelPropagation"];
|
||||
}
|
||||
else if(scrollLeft >= content_width - container_width && deltaX > 0 && deltaY == 0) {
|
||||
return !settings["wheelPropagation"];
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
$this.mousewheel(function(e, delta, deltaX, deltaY) {
|
||||
$this.scrollTop($this.scrollTop() - (deltaY * 10));
|
||||
$this.scrollLeft($this.scrollLeft() + (deltaX * 10));
|
||||
$this.scrollTop($this.scrollTop() - (deltaY * settings["wheelSpeed"]));
|
||||
$this.scrollLeft($this.scrollLeft() + (deltaX * settings["wheelSpeed"]));
|
||||
|
||||
// update bar position
|
||||
updateBarSizeAndPosition();
|
||||
|
||||
if(content_height > container_height || content_width > container_width) {
|
||||
if(shouldPreventDefault(deltaX, deltaY)) {
|
||||
e.preventDefault();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// bind mobile touch handler
|
||||
var bindMobileTouchHandler = function() {
|
||||
var applyTouchMove = function(difference_x, difference_y) {
|
||||
$this.scrollTop($this.scrollTop() - difference_y);
|
||||
$this.scrollLeft($this.scrollLeft() - difference_x);
|
||||
|
||||
// update bar position
|
||||
updateBarSizeAndPosition();
|
||||
};
|
||||
|
||||
var start_coords = {},
|
||||
start_time = 0,
|
||||
speed = {},
|
||||
breaking_process = null;
|
||||
|
||||
$this.bind("touchstart.perfect-scroll", function(e) {
|
||||
var touch = e.originalEvent.targetTouches[0];
|
||||
|
||||
start_coords.pageX = touch.pageX;
|
||||
start_coords.pageY = touch.pageY;
|
||||
|
||||
start_time = (new Date()).getTime();
|
||||
|
||||
if (breaking_process !== null) {
|
||||
clearInterval(breaking_process);
|
||||
}
|
||||
});
|
||||
$this.bind("touchmove.perfect-scroll", function(e) {
|
||||
var touch = e.originalEvent.targetTouches[0];
|
||||
|
||||
var current_coords = {};
|
||||
current_coords.pageX = touch.pageX;
|
||||
current_coords.pageY = touch.pageY;
|
||||
|
||||
var difference_x = current_coords.pageX - start_coords.pageX,
|
||||
difference_y = current_coords.pageY - start_coords.pageY;
|
||||
|
||||
applyTouchMove(difference_x, difference_y);
|
||||
start_coords = current_coords;
|
||||
|
||||
var current_time = (new Date()).getTime();
|
||||
speed.x = difference_x / (current_time - start_time);
|
||||
speed.y = difference_y / (current_time - start_time);
|
||||
start_time = current_time;
|
||||
|
||||
e.preventDefault();
|
||||
});
|
||||
$this.bind("touchend.perfect-scroll", function(e) {
|
||||
breaking_process = setInterval(function() {
|
||||
if(Math.abs(speed.x) < 0.01 && Math.abs(speed.y) < 0.01) {
|
||||
clearInterval(breaking_process);
|
||||
return;
|
||||
}
|
||||
|
||||
applyTouchMove(speed.x * 30, speed.y * 30);
|
||||
|
||||
speed.x *= 0.8;
|
||||
speed.y *= 0.8;
|
||||
}, 10);
|
||||
});
|
||||
};
|
||||
|
||||
var destroy = function() {
|
||||
$scrollbar_x.remove();
|
||||
$scrollbar_y.remove();
|
||||
$this.unbind('mousewheel');
|
||||
$this.unbind('touchstart.perfect-scroll');
|
||||
$this.unbind('touchmove.perfect-scroll');
|
||||
$this.unbind('touchend.perfect-scroll');
|
||||
$(window).unbind('mousemove.perfect-scroll');
|
||||
$(window).unbind('mouseup.perfect-scroll');
|
||||
$this.data('perfect_scrollbar', null);
|
||||
@@ -188,10 +292,13 @@
|
||||
$this.data('perfect_scrollbar_destroy', null);
|
||||
};
|
||||
|
||||
var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);
|
||||
|
||||
var initialize = function() {
|
||||
updateBarSizeAndPosition();
|
||||
bindMouseScrollXHandler();
|
||||
bindMouseScrollYHandler();
|
||||
if(isMobile) bindMobileTouchHandler();
|
||||
if($this.mousewheel) bindMouseWheelHandler();
|
||||
$this.data('perfect_scrollbar', $this);
|
||||
$this.data('perfect_scrollbar_update', updateBarSizeAndPosition);
|
||||
|
||||
Reference in New Issue
Block a user