Created
September 12, 2012 01:26
-
-
Save NdYAG/3703513 to your computer and use it in GitHub Desktop.
Remember where you've read on the webpage.Go there next time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
//if autoStart is false,press key(ctrl+shift+a) to remember position | |
//if autoStart is true,remember position when page is scrolled(how?) | |
var autoStart=false; | |
var xposition=window.pageXOffset; | |
var yposition=window.pageYOffset; | |
//press shortcut key to remember | |
if(!autoStart){ | |
document.addEventListener("keydown",function(){ | |
if((event.ctrlKey)&&(event.altKey)&&(event.keyCode==82)){ | |
localStorage.position=window.pageXOffset + "|" + window.pageYOffset; | |
alert("Position Remembered.\nNext time I'll bring you here."); | |
} | |
},false); | |
} | |
else{ | |
document.addEventListener("scroll",function(){ | |
if(Math.abs(window.pageYOffset-yposition)>100){ | |
localStorage.position=window.pageXOffset + "|" + window.pageYOffset; | |
xposition=window.pageXOffset; | |
yposition=window.pageYOffset; | |
} | |
},false); | |
} | |
//when open again,scroll to the remembered position | |
window.addEventListener("load",function(){ | |
var savedPosition=localStorage.position; | |
if(savedPosition){ | |
savedPosition=savedPosition.split("|"); | |
window.scrollTo(savedPosition[0],savedPosition[1]); | |
} | |
},false); | |
//delete saved position | |
document.addEventListener("keydown",function(){ | |
if((event.ctrlKey)&&(event.altKey)&&(event.keyCode==68)){ | |
localStorage.removeItem("position"); | |
alert("Deleted."); | |
} | |
},false); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment