ブロック要素の中で滑らかスクロールする方法(javascript)2
Posted on 8月 27, 2006
Filed Under javascript |
昨日の ブロック要素の中で滑らかスクロールする方法(javascript) はつくってて楽しかった♪
Flashアプリとかつくる人にとっては当たり前な気がしますが。
usage:
http://slightlyblue.com/lab/t20060827/smoothscroll.js を読み込んで、
SmoothScroll.scrollTo( ‘target id’, ‘parent id’, options);
‘parent id’ はスクロールしようとしているブロック要素のid
‘target id’ は ‘parent id’ブロック内にあるスクロール目標(=スクロール後にブロックの一番上に表示させたい行)のid
options は { time : 500, unit : 50 } 等と指定する
options.time は スクロール完了までの時間
options.unit は スクロール完了までに何ms単位で細かくスクロールするか
映画のフレームレート:24fps(40ms/frame)が映像を滑らかに見せるぎりぎりのラインだと思うので、それくらいを目安に。
point1:
滑らかなスクロールは、
$(’parent id’).scrollTop を options.unit 毎に更新することで実現します。
更新毎の移動量は、線形に漸減する方式になっています。
目標の距離に対して、options.time / options.unit 回に分けてスクロールするので、
目標の距離 / ( options.time / options.unit ) だけ一度に動かすと、速度変化は起きません。
線形に漸減させるためには、
一回の移動量 = 目標の距離 / ( options.time / options.unit ) * 2
x2すればいいのです。
この三角形を意識すれば簡単。
4分の1円形状に速度が落ちていったらかっこいいかな、なんて考えたりすると楽しい。。
point2:
もうひとつのポイントは、IE/Firefoxの違い
MSDN Home > 連載コラム > DHTML Dude > 配置と編集
HTML要素のサイズや位置を取得する
この辺りが詳しい。
offsetTopを使って、スクロールの目標を決めているのだけれど、
offsetTopがどこ基準のオフセットなのか、ブラウザ依存。
ここでは、’parent id’のwidth,heightを絶対値指定して、’parent id’ブロックをその内側にある’target id’のoffsetParentにしている。
もし万が一使うことがありそうだったらコメントいただけたらうれしいです。
Comments
Leave a Reply
ブロック要素の中で滑らかスクロールする方法(javascript)
Posted on 8月 27, 2006
Filed Under javascript |
ブロック要素の中で滑らかスクロールする方法(javascript)
IE,Firefoxのみで動作確認(IE追加@2006/08/27)
気持ちいいインターフェース。
Scriptaculous.Effects.ScrollTo をHackする方法もあるけれど、重いので自前で。
明日解説するかも。
a
b
c
d
e
f
g
h
[i]
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
[z]
-
javascript:
-
var SmoothScroll = {};
-
SmoothScroll = {
-
targetScrollTop : 0, // we're gonna make the $(parentid).scrollTop -> targetScrollTop
-
dist : 0,
-
timer : 0,
-
count : 0,
-
parentid : 0,
-
lastDist : 0,
-
//speedStore : [], // for debug
-
options : {},
-
defaultOptions : {
-
time : 1*1000, // [ms]
-
unit : 50 // [ms]
-
},
-
scrollTo : function( element, parent, options ){
-
this.options.time = this.defaultOptions.time;
-
this.options.unit = this.defaultOptions.unit;
-
if( options ){
-
this.options.time = ( options.time ) ? options.time : this.options.time;
-
this.options.unit = ( options.unit ) ? options.unit : this.options.unit;
-
}
-
clearInterval( this.timer );
-
this.parentid = parent;
-
-
this.scrollTopMax = this.$(parent).scrollHeight - this.$(parent).offsetHeight + parseInt(this.$(parent).style.borderTopWidth) + parseInt(this.$(parent).style.borderBottomWidth);
-
-
if( navigator.userAgent.match( "MSIE" ) ){
-
this.targetScrollTop = ( element ) ? this.$(element).offsetTop : 0;
-
}else{
-
var targetOffsetTop = ( element ) ? this.$(element).offsetTop : this.$(parent).offsetTop;
-
this.targetScrollTop = targetOffsetTop - this.$(parent).offsetTop;
-
}
-
this.targetScrollTop = ( this.targetScrollTop> this.scrollTopMax ) ? this.scrollTopMax : this.targetScrollTop;
-
-
this.dist = this.targetScrollTop - this.$(parent).scrollTop;
-
this.lastDist = 0;
-
this.timer = setInterval('SmoothScroll.update()', this.options.unit );
-
this.count = 0;
-
//this.speedStore = [];
-
this.update();
-
},
-
update : function(){
-
var dist = this.targetScrollTop - this.$(this.parentid).scrollTop;
-
var speed = 2 * dist * this.options.unit / ( this.options.time - this.options.unit * this.count );
-
//this.speedStore.push( speed );
-
speed = ( speed> 0 ) ? Math.ceil( speed ) : Math.floor( speed );
-
if( Math.abs(dist) <= Math.abs(speed) ){
-
// got there
-
clearInterval( this.timer );
-
this.$(this.parentid).scrollTop = this.targetScrollTop;
-
return;
-
}else if( this.lastDist == dist ){
-
// stuck
-
clearInterval( this.timer );
-
this.$(this.parentid).scrollTop = this.targetScrollTop;
-
return;
-
}
-
var scrollTop = this.$(this.parentid).scrollTop + speed;
-
this.$(this.parentid).scrollTop = scrollTop;
-
this.lastDist = dist;
-
this.count++;
-
if( this.count == this.options.time / this.options.unit ){
-
// timeout
-
clearInterval( this.timer );
-
this.$(this.parentid).scrollTop = this.targetScrollTop;
-
}
-
},
-
$ : function(id) {
-
return document.getElementById(id);
-
}
-
}
-
function gototop(){
-
document.getElementById('scroll20060827').scrollTop = 0;
-
}
-
-
html:
-
-
</p><p id="scroll20060827" style="border: 2px dotted black; width: 100px; height: 100px; overflow-y: scroll"> a
-
b
-
c
-
d
-
e
-
f
-
g
-
h
-
<span id="t20060827i" style="color: red"><strong>[i]</strong>
-
</span>j
-
k
-
l
-
m
-
n
-
o
-
p
-
q
-
r
-
s
-
t
-
u
-
v
-
w
-
x
-
y
-
<span id="t20060827z" style="color: blue"><strong>[z]</strong></span> <input value="go to top (normal)" onclick="gototop();" type="button" /> <input value="go to top smoothly" onclick="SmoothScroll.scrollTo(0,'scroll20060827');" type="button" /> <input value="go to [i] smoothly" onclick="SmoothScroll.scrollTo('t20060827i','scroll20060827');" type="button" /> <input value="go to [z] smoothly" onclick="SmoothScroll.scrollTo('t20060827z','scroll20060827');" type="button" /> <input value="go to [i] smoothly, but FAST" onclick="SmoothScroll.scrollTo('t20060827i','scroll20060827',{time: 500});" type="button" /> <input value="go to [z] smoothly, but FAST" onclick="SmoothScroll.scrollTo('t20060827z','scroll20060827',{time: 500});" type="button" /> <input value="go to [i] smoothly, but SLOW" onclick="SmoothScroll.scrollTo('t20060827i','scroll20060827',{time: 2000});" type="button" /> <input value="go to [z] smoothly, but SLOW" onclick="SmoothScroll.scrollTo('t20060827z','scroll20060827',{time: 2000});" type="button" />
Comments
One Response to “ブロック要素の中で滑らかスクロールする方法(javascript)”
Leave a Reply
ブロック要素の中で滑らかスクロールする方法(javascript)2
昨日の ブロック要素の中で滑らかスクロールする方法(javascript) は…