Skip to content

Instantly share code, notes, and snippets.

@karlcow
Created December 19, 2019 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlcow/d1c9b294a4ebd08338b26fce4f9a4544 to your computer and use it in GitHub Desktop.
Save karlcow/d1c9b294a4ebd08338b26fce4f9a4544 to your computer and use it in GitHub Desktop.
(function ($) {
var height = $.fn.height,
width = $.fn.width,
left = $.fn.left,
top = $.fn.top;
$.fn.extend({
width: function () {
if (!this[0]) error();
if (this[0] == window) return self.innerWidth || $.boxModel && document.documentElement.clientWidth || document.body.clientWidth;
if (this[0] == document) return ((document.documentElement && document.compatMode == 'CSS1Compat') ? document.documentElement.scrollWidth : document.body.scrollWidth);
return width.apply(this, arguments)
},
height: function () {
if (!this[0]) error();
if (this[0] == window) return self.innerHeight || $.boxModel && document.documentElement.clientHeight || document.body.clientHeight;
if (this[0] == document) return ((document.documentElement && document.compatMode == 'CSS1Compat') ? document.documentElement.scrollHeight : document.body.scrollHeight);
return height.apply(this, arguments)
},
left: function () {
if (!this[0]) return true;
var obj = document.getElementById ? document.getElementById(this[0].id) : document.all(this[0].id);
var tagCoords = new Object();
tagCoords.x = obj.offsetLeft;
while ((obj = obj.offsetParent) != null) {
tagCoords.x += obj.offsetLeft
}
if ((tagCoords.x * 0) == 0) return (tagCoords.x);
else return (this[0].id)
},
top: function () {
if (!this[0]) return true;
var obj = document.getElementById ? document.getElementById(this[0].id) : document.all(this[0].id);
var tagCoords = new Object();
tagCoords.y = obj.offsetTop;
while ((obj = obj.offsetParent) != null) {
tagCoords.y += obj.offsetTop
}
if ((tagCoords.y * 0) == 0) return (tagCoords.y);
else return (this[0].id)
}
})
}) (jQuery);
$(function () {
$('a[href^="#"], area[href^="#"]').not('.nopscr a[href^="#"], a[href^="#"].nopscr').click(function () {
var usrUrl = location.hostname + location.pathname;
var anchorPath = ((this.href).substring(0, (((this.href).length) - ((this.hash).length)))).slice((this.href).indexOf('//') + 2);
if (usrUrl.lastIndexOf('?') != - 1) usrUrlOmitQ = usrUrl.slice(0, (usrUrl.lastIndexOf('?')));
else usrUrlOmitQ = usrUrl;
if (anchorPath.lastIndexOf('?') != - 1) anchorPathOmitQ = anchorPath.slice(0, (anchorPath.lastIndexOf('?')));
else anchorPathOmitQ = anchorPath;
if (anchorPathOmitQ == usrUrlOmitQ) {
coliss.toAnchor((this.hash).substr(1));
return false
}
});
$('body').click(function () {
coliss.stopScroll()
})
});
this.pageScrollTimer = null;
var coliss = {
getScrollRange: function (type) {
if (type == 'x') return (($(document).width()) - ($(window).width()));
else if (type == 'y') return (($(document).height()) - ($(window).height()))
},
getWindowOffset: function (type) {
if (type == 'x') return (window.pageXOffset || document.body.scrollLeft || document.body.parentNode.scrollLeft);
else if (type == 'y') return (window.pageYOffset || document.body.scrollTop || document.body.parentNode.scrollTop)
},
pageScroll: function (toX, toY, frms, frX, frY) {
var pageScrollTimer;
if (pageScrollTimer) clearTimeout(pageScrollTimer);
var spd = 16 * adjSpeed;
var actX = coliss.getWindowOffset('x');
var actY = coliss.getWindowOffset('y');
if (!toX || toX < 0) toX = 0;
if (!toY || toY < 0) toY = 0;
if (!frms) frms = $.browser.mozilla ? 10 : $.browser.opera ? 8 : 9;
if (!frX) frX = 0 + actX;
if (!frY) frY = 0 + actY;
frX += (toX - actX) / frms;
if (frX < 0) frX = 0;
frY += (toY - actY) / frms;
if (frY < 0) frY = 0;
var posX = Math.ceil(frX);
var posY = Math.ceil(frY);
window.scrollTo(posX, posY);
if ((Math.floor(Math.abs(actX - toX)) < 1) && (Math.floor(Math.abs(actY - toY)) < 1)) {
clearTimeout(this.pageScrollTimer);
window.scroll(toX, toY)
} else if ((posX != toX) || (posY != toY)) this.pageScrollTimer = setTimeout('coliss.pageScroll(' + toX + ',' + toY + ',' + frms + ',' + frX + ',' + frY + ')', spd);
else clearTimeout(this.pageScrollTimer)
},
stopScroll: function () {
clearTimeout(this.pageScrollTimer)
},
cancelScroll: function (e) {
coliss.stopScroll()
},
toAnchor: function (idName) {
coliss.stopScroll();
var anchorX,
anchorY;
if (!!idName) {
if (idName == virtualTopId) {
anchorX = (virtualTop == 0) ? 0 : (virtualTop == 1) ? window.pageXOffset || document.body.scrollLeft || document.body.parentNode.scrollLeft : $('#' + idName).left();
anchorY = ((virtualTop == 0) || (virtualTop == 1)) ? 0 : $('#' + idName).top()
} else {
anchorX = (adjTraverser == 0) ? 0 : (adjTraverser == 1) ? ($('#' + idName).left()) : window.pageXOffset || document.body.scrollLeft || document.body.parentNode.scrollLeft;
anchorY = adjPosition ? ($('#' + idName).top()) + adjPosition : ($('#' + idName).top())
}
var dMaxX = coliss.getScrollRange('x');
var dMaxY = coliss.getScrollRange('y');
if (((anchorX * 0) == 0) || ((anchorY * 0) == 0)) {
var setX = (anchorX < 1) ? 0 : (anchorX > dMaxX) ? dMaxX : anchorX;
var setY = (anchorY < 1) ? 0 : (anchorY > dMaxY) ? dMaxY : anchorY;
coliss.pageScroll(setX, setY)
} else location.hash = idName
} else coliss.pageScroll(0, 0)
},
initPageScroller: function () {
var usrUrl = location.href;
var checkAnchor = usrUrl.indexOf('#', 0);
var checkPageScroller = usrUrl.match(callExternal);
usrUrl = usrUrl.replace(/</g, '&lt;');
usrUrl = usrUrl.replace(/>/g, '&gt;');
if (!!checkPageScroller) {
anchorId = usrUrl.slice(usrUrl.lastIndexOf('?' + callExternal) + 4, usrUrl.length);
timerID = setTimeout('coliss.toAnchor(anchorId)', delayExternal)
}
if (!checkAnchor) window.scroll(0, 0);
else return true
}
};
$(coliss.initPageScroller);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment