mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 18:32:22 +00:00
add page flip
This commit is contained in:
parent
07b01e3724
commit
0adf5ed2c7
81
js/ditto.js
81
js/ditto.js
@ -17,10 +17,6 @@ var ditto = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var disqusCode = '<h3>留言</h3><div id="disqus_thread"></div>';
|
var disqusCode = '<h3>留言</h3><div id="disqus_thread"></div>';
|
||||||
var header,
|
|
||||||
headerHeight = 30,
|
|
||||||
treshold = 0,
|
|
||||||
lastScroll = 0;
|
|
||||||
|
|
||||||
function initialize() {
|
function initialize() {
|
||||||
// initialize sidebar and buttons
|
// initialize sidebar and buttons
|
||||||
@ -39,16 +35,14 @@ function initialize() {
|
|||||||
// page router
|
// page router
|
||||||
router();
|
router();
|
||||||
$(window).on('hashchange', router);
|
$(window).on('hashchange', router);
|
||||||
$(document).on('scroll', function (evt) {
|
var flip = document.querySelector('#flip');
|
||||||
var newScroll = $(document).scrollTop(),
|
var hasTouch = 'ontouchstart' in window;
|
||||||
diff = newScroll-lastScroll;
|
|
||||||
treshold = (treshold+diff>headerHeight) ? headerHeight : treshold+diff;
|
|
||||||
treshold = (treshold < 0) ? -10 : treshold;
|
|
||||||
|
|
||||||
$('#flip').css('bottom', (-treshold)+'px');
|
|
||||||
|
|
||||||
lastScroll = newScroll;
|
if (!hasTouch) {
|
||||||
});
|
Veil(flip, {
|
||||||
|
prop: 'bottom'
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function init_sidebar_section() {
|
function init_sidebar_section() {
|
||||||
@ -246,3 +240,64 @@ function router() {
|
|||||||
$(ditto.loading_id).hide();
|
$(ditto.loading_id).hide();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// veil.js
|
||||||
|
|
||||||
|
(function (window, document, $) {
|
||||||
|
|
||||||
|
window.Veil = function (elem, options) {
|
||||||
|
|
||||||
|
var defaults = {
|
||||||
|
offset: 0,
|
||||||
|
prop: 'top',
|
||||||
|
onScroll: function (elem, threshold) {},
|
||||||
|
isHidden: function (elem, threshold) {},
|
||||||
|
isVisible: function (elem, threshold) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
var lastScroll = 0;
|
||||||
|
var treshold = 0;
|
||||||
|
|
||||||
|
// merge the defaults with the passed options
|
||||||
|
for (var key in options) {
|
||||||
|
defaults[key] = options[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the defaults.prop is 'top' or 'bottom'
|
||||||
|
// we need the height, otherwise the width
|
||||||
|
var sizeProp = /to/i.test(defaults.prop) ? 'offsetHeight' : 'offsetWidth';
|
||||||
|
|
||||||
|
Veil.scroll = function () {
|
||||||
|
var scrollOffset = document.documentElement.scrollTop || document.body.scrollTop,
|
||||||
|
elemSize = elem[sizeProp] + defaults.offset,
|
||||||
|
diff = scrollOffset - lastScroll;
|
||||||
|
|
||||||
|
treshold = (treshold + diff > elemSize) ? elemSize : (scrollOffset < 0) ? 0 : treshold + diff;
|
||||||
|
treshold = (treshold < 0) ? 0 : treshold;
|
||||||
|
|
||||||
|
elem.style[defaults.prop] = (-treshold) + 'px';
|
||||||
|
defaults.onScroll(elem, treshold);
|
||||||
|
if (!treshold) defaults.isVisible(elem, treshold);
|
||||||
|
if (treshold === elemSize) defaults.isHidden(elem, treshold);
|
||||||
|
|
||||||
|
lastScroll = scrollOffset;
|
||||||
|
};
|
||||||
|
|
||||||
|
Veil.scroll();
|
||||||
|
|
||||||
|
// modern browsers
|
||||||
|
if (window.addEventListener) return window.addEventListener('scroll', Veil.scroll);
|
||||||
|
|
||||||
|
// ie8 and below
|
||||||
|
window.attachEvent('onscroll', Veil.scroll);
|
||||||
|
};
|
||||||
|
|
||||||
|
// if jquery or zepto is present,
|
||||||
|
// add a function to their prototype
|
||||||
|
if ($) $.fn.veil = function (options) {
|
||||||
|
return this.each(function () {
|
||||||
|
Veil(this, options);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
}(this, document, this.jQuery || this.Zepto));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user