Skip to content

Instantly share code, notes, and snippets.

@netsi1964
Last active August 29, 2015 14:05
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 netsi1964/4ddffe1ae14e05220d25 to your computer and use it in GitHub Desktop.
Save netsi1964/4ddffe1ae14e05220d25 to your computer and use it in GitHub Desktop.
This gist will reveal and handle scrolling of all window content in an animated way using jQuery
var iLeft = Math.abs(parseInt(window.screen.availLeft*Math.random()));
var iTop = Math.abs(parseInt(window.screen.availTop*Math.random()));
var sLog = "Trying to scroll to ("+iLeft+","+iTop+")";
var $animatedElements = $("html, body");
var iNoOfAnimatedElements = $animatedElements.length;
$animatedElements.each(function() {
var $this = $(this);
$this.data({"scrolltop":this.scrollTop, "scrollleft":this.scrollLeft})
}).animate({ scrollLeft: iLeft, scrollTop: iTop }, 1000, function(e) {
var $this = $(this);
var currLeft = this.scrollLeft;
var currTop = this.scrollTop;
var sAnimationWorking = ((iLeft!=currLeft || iTop!=currTop) ? " NOT ": "");
if (sAnimationWorking!=="") {
console.log("Working on "+this.tagName);
}
sLog+="\n("+$this.data("scrollleft")+", "+$this.data("scrolltop")+") -> ("+currLeft+", "+currTop+") so "+this.tagName+" can "+sAnimationWorking+"be used to move content on current browser";
iNoOfAnimatedElements--;
if (iNoOfAnimatedElements===0) console.log(sLog)
})
function scrollWindowTo(options, fCallback) {
options = options || {};
options = {
"left": (typeof options.left !== "undefined") ? options.left : Math.abs(parseInt(window.screen.availLeft * Math.random())),
"top": (typeof options.top !== "undefined") ? options.top : Math.abs(parseInt(window.screen.availTop * Math.random())),
"speed": (typeof options.speed !== "undefined") ? options.speed : 1000,
"callback": (typeof fCallback !== "undefined") ? fCallback : null,
"log": (typeof options.log !== "undefined") ? options.log : false,
"noOfCallBack": 0
};
// Are we allready at the wanted coordinate? Then exit (with callback)
if (options.left == window.scrollX && options.top == window.scrollY) {
// alert("you are allready there!")
if (options.callback !== null) {
// alert("did callback - nothing done")
options.callback.call(this, options);
}
return false
}
var sLog = "Trying to scroll to (" + options.left + "," + options.top + ")";
var $animatedElements = $("html, body");
var iNoOfAnimatedElements = $animatedElements.length;
$animatedElements.each(function () {
var $this = $(this);
$this.data({
"scrolltop": this.scrollTop,
"scrollleft": this.scrollLeft
});
}).animate({
scrollLeft: options.left,
scrollTop: options.top
}, options.speed, function () {
var $this = $(this);
var currLeft = this.scrollLeft;
var currTop = this.scrollTop;
var sAnimationWorking = ((options.left !== currLeft || options.top !== currTop) ? " NOT " : "");
if (sAnimationWorking !== "") {
if (options.log) {
console.log("Working on " + this.tagName);
}
if (options.callback !== null && options.noOfCallBack===0) {
// alert("did callback normally "+this.tagName+"\n(" + options.left + "," + options.top + ") (" + currLeft + "," + currTop + ")")
options.callback.call(this, options);
options.noOfCallBack++;
};
}
iNoOfAnimatedElements--;
// alert("(" + options.left + "," + options.top + ") (" + currLeft + "," + currTop + ")")
if (options.log) {
sLog += "\n(" + $this.data("scrollleft") + ", " + $this.data("scrolltop") + ") -> (" + currLeft + ", " + currTop + ") so " + this.tagName + " can " + sAnimationWorking + "be used to move content on current browser";
}
if (iNoOfAnimatedElements <= 0) {
if (options.log) {
console.log(sLog);
}
}
});
}
//scrollWindowTo({}, function (a) {
// alert("Done" + a)
//});
@netsi1964
Copy link
Author

scrollWindowTo(oOptions, fCallback) is a function which can be called if you want to scroll the content on a page. You can see the code for parameters, but you will be able to call it with any parameters for testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment