Created
October 3, 2011 15:17
-
-
Save justoneplanet/1259350 to your computer and use it in GitHub Desktop.
overflow: scrollの時にsmooth scrollする簡易プラグイン
This file contains hidden or 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
$.fn.boxScroll = function(binder, target){ | |
if(binder.attr('data-locked') === 'true'){ | |
return; | |
} | |
this.animate( | |
{"scrollTop" : target.positionIn(binder).top}, | |
1500, | |
"easeOutExpo", | |
function(){ | |
binder.attr('data-locked', 'false'); | |
} | |
); | |
binder.attr('data-locked', 'true'); | |
}; |
This file contains hidden or 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
// elmにたどり着くまでの要素にposition : relativeがあると上手く動作しないはず | |
// 先祖ノードに含まれないノードの時の処理も書かないとな | |
$.fn.positionIn = function(elm){ | |
var pos = elm.css("position"); | |
elm.css({"position" : "relative"}); | |
var current = this; | |
var left = 0; | |
var top = 0; | |
var counter = 0; | |
while(current.get(0) !== elm.get(0) && current.parent()){ | |
left += current.position().left; | |
top += current.position().top; | |
current = $(current.get(0).parentNode); | |
if(++counter > 10000){break;} | |
} | |
elm.css({"position" : pos}); | |
return {"left" : left, "top" : top}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment