diff --git a/examples/options-autoupdate.html b/examples/options-autoupdate.html new file mode 100644 index 0000000..cecae6c --- /dev/null +++ b/examples/options-autoupdate.html @@ -0,0 +1,55 @@ + + + + + perfect-scrollbar example + + + + + +
+
+
+
+

+ + +

+ + + + diff --git a/src/js/lib/helper.js b/src/js/lib/helper.js index a9f0163..609b345 100644 --- a/src/js/lib/helper.js +++ b/src/js/lib/helper.js @@ -23,6 +23,26 @@ var clone = exports.clone = function (obj) { } }; +exports.debounce = function (func, wait, immediate) { + var timeout; + return function () { + var context = this; + var args = arguments; + var later = function () { + timeout = null; + if (!immediate) { + func.apply(context, args); + } + }; + var callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if (callNow) { + func.apply(context, args); + } + }; +}; + exports.extend = function (original, source) { var result = clone(original); for (var key in source) { diff --git a/src/js/plugin/autoupdate.js b/src/js/plugin/autoupdate.js new file mode 100644 index 0000000..d78dc35 --- /dev/null +++ b/src/js/plugin/autoupdate.js @@ -0,0 +1,23 @@ +'use strict'; + +var update = require('./update'); +var MutationObserver = window.MutationObserver; +var instances = require('./instances'); + +module.exports = function (element) { + if (MutationObserver === null || MutationObserver === undefined) { + // MutationObserver is not supported + return; + } + + var i = instances.get(element); + var onMutationObserver = function () { + update(element); + }; + + i.observer = new MutationObserver(onMutationObserver); + onMutationObserver(); + + var config = { childList: true, subtree: true }; + i.observer.observe(element, config); +}; diff --git a/src/js/plugin/default-setting.js b/src/js/plugin/default-setting.js index b3f2ddd..1b907ed 100644 --- a/src/js/plugin/default-setting.js +++ b/src/js/plugin/default-setting.js @@ -12,5 +12,6 @@ module.exports = { useBothWheelAxes: false, wheelPropagation: false, wheelSpeed: 1, - theme: 'default' + theme: 'default', + autoupdate: true }; diff --git a/src/js/plugin/destroy.js b/src/js/plugin/destroy.js index 97a83e0..24419cf 100644 --- a/src/js/plugin/destroy.js +++ b/src/js/plugin/destroy.js @@ -11,6 +11,10 @@ module.exports = function (element) { return; } + if (i.observer) { + i.observer.disconnect(); + } + i.event.unbindAll(); dom.remove(i.scrollbarX); dom.remove(i.scrollbarY); diff --git a/src/js/plugin/initialize.js b/src/js/plugin/initialize.js index 05918a5..ccaf834 100644 --- a/src/js/plugin/initialize.js +++ b/src/js/plugin/initialize.js @@ -4,6 +4,8 @@ var _ = require('../lib/helper'); var cls = require('../lib/class'); var instances = require('./instances'); var updateGeometry = require('./update-geometry'); +var autoupdate = require('./autoupdate'); +var resizer = require('./resizer'); // Handlers var handlers = { @@ -34,4 +36,9 @@ module.exports = function (element, userSettings) { nativeScrollHandler(element); updateGeometry(element); + + if (i.settings.autoupdate) { + autoupdate(element); + resizer(element); + } }; diff --git a/src/js/plugin/resizer.js b/src/js/plugin/resizer.js new file mode 100644 index 0000000..74074ea --- /dev/null +++ b/src/js/plugin/resizer.js @@ -0,0 +1,15 @@ +'use strict'; + +var update = require('./update'); +var instances = require('./instances'); +var _ = require('../lib/helper'); + +module.exports = function (element) { + var i = instances.get(element); + + var onResize = function () { + update(element); + }; + + i.event.bind(window, 'resize', _.debounce(onResize, 60)); +};