kurubusi.slider.js
ドロワーメニューやスワイプギャラリー等に使えるスライダースクリプトです。
勿論レスポンシブなので、スマホではスワイプで操作可能、PCではマウスムーブをマウスホイールに対応しています。
注:スライダーの部分のみのスクリプトです。
特記する特徴
同一ページ内で複数使用できます。
IDを変えれば同一ページ内で複数使えます。
レスポンシブ対応
PCではマウスイベント・ホイールイベント、スマホではタッチイベントで操作できます。
イージングがちょっといい感じ
ただスクロールするだけではなく、手を離した後にも惰性でいい感じに進みます。
KURUBUSISLIDER コード
コピペしてそのまま使えます。
呼び出し
target
- 実際にスクロールするオブジェクトのIDかオブジェクトを指定必須
parlent
- スクロールするオブジェクトの基準となるオブジェクトのIDかオブジェクトを指定。
parlent
がrelativeになってそこを基準にtarget
がabsoluteで動きます。必須
オプション
speedrate
- 惰性で進むときのスピードレートを指定。デフォルトで150
durationrateY or durationrateX
- 惰性で進む距離のレートを指定。デフォルトで5
y or x
- 縦(y)にスクロールするか横(x)にスクロールするか指定。省略時は縦(y)
CSS
#w { //parlentになるオブジェクトにはheightを設定して下さい |
#c { //x方向にスクロースする時はtargetにwidthを指定して下さい。 |
KURUBUSISLIDER
コピペしてそのまま使えます。
var KURUBUSISLIDER = function (obj){ |
args = Array.prototype.slice.call(arguments); |
this .w = (args[0].parlent && typeof args[0].parlent === "string" ) ? document.getElementById(args[0].parlent) : args[0].parlent; |
this .c = (args[0].target && typeof args[0].target === "string" ) ? document.getElementById(args[0].target) : args[0].target; |
this .speedrate = args[0].speedrate || 150; |
this .durationrateY = args[0].durationrateY || 5; |
this .durationrateX = args[0].durationrateX || 5; |
this .direction = args[0].direction || 'y' ; |
var objstyleheight = this .objtComputedStyle( this .c, 'height' ), |
objstylewidth = this .objtComputedStyle( this .c, 'width' ), |
wheight = this .objtComputedStyle( this .w, 'height' ), |
wwidth = this .objtComputedStyle( this .w, 'width' ); |
this .w.style.overflow = 'hidden' ; |
this .w.style.position = 'relative' ; |
this .c.style.position = 'absolute' ; |
this .overdiv = document.createElement( 'div' ); |
this .overdiv.style.width = (parseFloat(document.defaultView.getComputedStyle( this .c, '' )[ 'width' ])) + 'px' ; |
this .overdiv.style.height = (parseFloat(document.defaultView.getComputedStyle( this .c, '' )[ 'height' ])) + 'px' ; |
this .overdiv.style.position = 'absolute' ; |
this .overdiv.style.top = '0' ; |
this .overdiv.style.left = '0' ; |
this .c.appendChild( this .overdiv); |
if ( this .direction === 'y' && objstyleheight < wheight){ |
this .c.style.paddingBottom = (wheight - (parseFloat(document.defaultView.getComputedStyle( this .c, '' )[ 'height' ]))) + 'px' ; |
this .managedPCdrag( this .c); |
this .managedPCwheel( this .c); |
Slider.prototype.managedPCwheel = function (target){ |
mousewheelevent = 'onwheel' in document ? 'wheel' : 'onmousewheel' in document ? 'mousewheel' : 'DOMMouseScroll' ; |
addEventSet(target, mousewheelevent, function (event){ |
addEventSet(target, mousewheel, function (event){ |
var onWheel = function (event){ |
if (!event) event = window.event; |
var delta = event.deltaY ? -(event.deltaY) : event.wheelDelta ? event.wheelDelta : -(event.detail), |
objstylewidth = (this_.objtComputedStyle(target, 'width' ) || 0), |
objstyleheight = (this_.objtComputedStyle(target, 'height' ) || 0); |
objstyletop = (this_.objtComputedStyle(target, 'top' ) || 0), |
objstyleleft = (this_.objtComputedStyle(target, 'left' ) || 0); |
begin = ( new Date()).getTime(); |
'touchMoveY' : objstyletop + delta, |
'touchStartY' : objstyletop, |
'touchMoveX' : objstyleleft, |
'touchStartX' : objstyleleft, |
'd_time' : ((( new Date()).getTime()) - begin) |
this_.touchingMove(moveingObj); |
Slider.prototype.managedPCdrag = function (target){ |
addEventSet(target, "mousedown" , function (event) { |
begin = ( new Date()).getTime(); |
touchStartX = event.pageX || (event.clientX + document.documentElement.scrollLeft); |
touchStartY = event.pageY || (event.clientY + document.documentElement.scrollTop); |
touchLayerX = event.layerX || event.offsetX; |
touchLayerY = event.layerY || event.offsetY; |
addEventSet(target, "mousemove" , function (event) { |
'touchMoveY' : event.pageY, |
'touchStartY' : touchStartY, |
'touchMoveX' : event.pageX, |
'touchStartX' : touchStartX, |
'touchLayerX' : touchLayerX, |
'touchLayerY' : touchLayerY, |
'd_time' : ((( new Date()).getTime()) - begin) |
this_.touchingMove(moveingObj); |
addEventSet(target, "mouseout" , function (event) { |
this_.touchdMove(moveingObj); |
addEventSet(target, "mouseup" , function (event) { |
this_.touchdMove(moveingObj); |
Slider.prototype.managedMB = function (target){ |
addEventSet(target, "touchstart" , function (event) { |
begin = ( new Date()).getTime(); |
touchStartX = event.touches[0].pageX; |
touchStartY = event.touches[0].pageY; |
touchLayerX = event.changedTouches[0].pageX - target.offsetLeft; |
touchLayerY = event.changedTouches[0].pageY - target.offsetTop; |
addEventSet(target, "touchmove" , function (event) { |
'touchMoveY' : event.changedTouches[0].pageY, |
'touchStartY' : touchStartY, |
'touchMoveX' : event.changedTouches[0].pageX, |
'touchStartX' : touchStartX, |
'touchLayerX' : touchLayerX, |
'touchLayerY' : touchLayerY, |
'd_time' : ((( new Date()).getTime()) - begin) |
this_.touchingMove(moveingObj); |
addEventSet(target, "touchcancel" , function (event) { |
this_.touchdMove(moveingObj); |
addEventSet(target, "touchend" , function (event) { |
this_.touchdMove(moveingObj); |
Slider.prototype.touchingMove = function (moveobj){ |
lastobj = moveobj[moveobj.length-1], |
objstyletop = ( this .objtComputedStyle(lastobj.target, 'top' ) || 0), |
objstyleleft = ( this .objtComputedStyle(lastobj.target, 'left' ) || 0), |
objstylebottom = ( this .objtComputedStyle(lastobj.target, 'bottom' ) || 0), |
objstyleright = ( this .objtComputedStyle(lastobj.target, 'right' ) || 0), |
objstylewidth = ( this .objtComputedStyle(lastobj.target, 'width' ) || 0), |
objstyleheight = ( this .objtComputedStyle(lastobj.target, 'height' ) || 0), |
wwidth = this .objtComputedStyle( this .w, 'width' ), |
wheight = this .objtComputedStyle( this .w, 'height' ), |
moveY = lastobj.touchMoveY - lastobj.touchLayerY; |
moveX = lastobj.touchMoveX - lastobj.touchLayerX; |
if ( this .direction === 'x' ){ |
if (wwidth - objstylewidth > moveX){ |
moveX = wwidth - objstylewidth; |
lastobj.target.style.left = moveX + 'px' ; |
if (wheight - objstyleheight > moveY){ |
moveY = wheight - objstyleheight; |
lastobj.target.style.top = moveY + 'px' ; |
Slider.prototype.touchdMove = function (moveobj){ |
lastobj = moveobj[moveobj.length-1], |
moveobjlength = moveobj.length, |
objstyletop = ( this .objtComputedStyle(lastobj.target, 'top' ) || 0), |
objstyleleft = ( this .objtComputedStyle(lastobj.target, 'left' ) || 0), |
objstylewidth = ( this .objtComputedStyle(lastobj.target, 'width' ) || 0), |
objstyleheight = ( this .objtComputedStyle(lastobj.target, 'height' ) || 0), |
wwidth = this .objtComputedStyle( this .w, 'width' ), |
wheight = this .objtComputedStyle( this .w, 'height' ), |
for ( var i = 1; i <= 10; i++){ |
if (moveobj[moveobjlength - i]){ |
recently.unshift(moveobj[moveobjlength - i]); |
dr = Math.sqrt(Math.pow((recently[recently.length-1].touchMoveX-recently[0].touchMoveX),2) + Math.pow((recently[recently.length-1].touchMoveY-recently[0].touchMoveY),2)); |
speed = dr / (recently[recently.length-1].d_time - recently[0].d_time); |
duration = speed * this .speedrate; |
moveX = (recently[recently.length-1].touchMoveX - recently[0].touchMoveX) * this .durationrateX; |
moveY = (recently[recently.length-1].touchMoveY - recently[0].touchMoveY) * this .durationrateY; |
toX = objstyleleft + moveX; |
toY = objstyletop + moveY; |
if (duration && duration > 100){ |
var timer = setInterval( function (){ |
var time = new Date() - begin, |
cuY = easeOutQuad(time, objstyletop, moveY, duration), |
cuX = easeOutQuad(time, objstyleleft, moveX, duration); |
if (this_.direction === 'x' ){ |
if (wwidth - objstylewidth > cuX){ |
cuX = wwidth - objstylewidth; |
lastobj.target.style.left = cuX + 'px' ; |
if (wheight - objstyleheight > cuY){ |
cuY = wheight - objstyleheight; |
lastobj.target.style.top = cuY + 'px' ; |
Slider.prototype.stopEvent = function (event){ |
if (event.preventDefault) { |
event.returnValue = false ; |
Slider.prototype.objtComputedStyle = function (obj, properties){ |
return parseFloat((obj.currentStyle || document.defaultView.getComputedStyle(obj, '' ))[properties]); |
switchArea = function (serchclass, serchtag) { |
reg = new RegExp( '(^| +)' + serchclass + '($| +)' ), |
if (serchtag === undefined) { |
var el = par.getElementsByTagName(serchtag); |
for ( var i = 0; i < el.length; i++) { |
if (reg.test(el[i].className)){ |
addEventSet = function (elm,listener,fn){ |
try { elm.addEventListener(listener,fn, false );} |
catch (e){ elm.attachEvent( "on" +listener,fn);}; |
extendDeep = function (parent, child){ |
toStr = Object.prototype.toString, |
if (parent.hasOwnProperty(i)){ |
if ( typeof parent[i] === "object" ){ |
child[i] = (toStr.call(parent[i]) === astr) ? [] : {}; |
extendDeep(parent[i], child[i]); |
addEventSet = function (elm,listener,fn){ |
try { elm.addEventListener(listener,fn, false );} |
catch (e){ elm.attachEvent( "on" +listener,fn);}; |
addReadyFunction = function (func){ |
if (document.addEventListener){ |
document.addEventListener( "DOMContentLoaded" , func , false ) ; |
} else if (window.ActiveXObject){ |
var ScrollCheck = function (){ |
document.documentElement.doScroll( "left" ); |
setTimeout(ScrollCheck , 1 ); |
var randam = Math.floor(Math.random()*1000) |
var time = date.getTime(); |
return randam + time.toString(); |
return Object.prototype.toString.call(o) === '[object Array]' ; |
easing = function (t,b,c,d){ |
if ((t/=d/2) < 1) return c/2*t*t*t*t + b; |
return -c/2 * ((t-=2)*t*t*t - 2) + b; |
easeOutQuad = function (t, b, c, d) { |
return -c *(t/=d)*(t-2) + b; |
addReadyFunction( function (){ |
更新履歴
- 2014/10/26
- 1.0.02_publicbeta公開。