ページ 縦にスクロール|Page Vertical Scroll|HTML5

ページ 縦にスクロール|Page Vertical Scroll|HTML5

ページ 縦にスクロール|Page Vertical Scroll|HTML5(HTML5, JavaScript, CSS3) : デモ

ページ 縦にスクロール|Page Vertical Scroll|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(6kb)

画面内のフリックで上下にスクロールできます。

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>Canvas Scroll</title>
        <link href="css/base.css" rel="stylesheet" type="text/css">
        <script src="js/devicecheck.js"></script>
        <script src="js/base.js"></script>
    </head>
    <body>
        <div id="contents">
            <canvas id="canvas" width="275" height="2000"></canvas>
        </div>
    </body>
</html>

2.CSS

@charset "utf-8";
   
body
{
    margin: 0;
    padding: 0;
    background-color: #fff;
}
   
#contents
{
    position: absolute;
    top: 0;
    left: 0;
    width: 275px;
    height: 275px;
    border: 1px solid #000;
    overflow:hidden;
}
   
#canvas
{
    position: absolute;
    top: 0;
    left: 0;
    width: 275px;
    height: 2000px;
}

3.JavaScript

base.js

window.addEventListener("load", init, false);
   
function init()
{
    var offsetX = 0,
    offsetY = 0,
    ctx,
    mouseX = 0,
    mouseY = 0,
    buttonDown,
    buttonMove,
    buttonUp,
    targetY = 0,
    friction = 0.98,
    canvasTop = 0,
    canvas,
    motion = null,
    moveY = 0,
    mapTop = 0,
    sy = 0,
    my = 0,
    vy = 0,
    vTotal = 0,
    topMax = 0,
    count = 0,
    timerCount,
    inter = 4,
    theContents;
      
    theContents = document.getElementById("contents");
    offsetX = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).width;
    offsetX = Number(offsetX.replace('px', ''));
   
    offsetY = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).height;
    offsetY = Number(offsetY.replace('px', ''));
     
    ctx = document.getElementById('canvas').getContext("2d");
     
    //ボタンタイプ
    if(g_mobiledevice === true)
    {
        buttonDown = 'touchstart';
        buttonMove = 'touchmove';
        buttonUp = 'touchend';
    }
    else
    {
        buttonDown = 'mousedown';
        buttonMove = 'mousemove';
        buttonUp = 'mouseup';
    }
      
    document.getElementById('canvas').addEventListener('touchstart', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchmove', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchend', function() 
    {
        event.preventDefault();
    });
     
    canvas = document.getElementById('canvas');
     
    var height = (canvas.currentStyle || document.defaultView.getComputedStyle(canvas,'')).height;
    height = Number(height.replace('px', ''));
     
    topMax = -height + 275;
    
    canvas.addEventListener(buttonDown, buttonDownHandler, false);
    canvas.addEventListener(buttonUp, buttonUpHandler, false);
     
    //全体を塗りつぶす
    ctx.fillStyle = "#DFDFDF";
    ctx.fillRect(0, 0, 275, height);
     
    //矩形描画
    for(var i = 0; i < 20; i++)
    {
        ctx.fillStyle = "#000000";
        ctx.fillRect(0, i * 100, 275, 50);
    }
     
    //slideAnimationY();
     
    function slideAnimationY()
    {
        animation();
        motion = requestAnimationFrame(slideAnimationY);
    }
     
    function countDown()
    {
        count++;
    }
     
    function buttonDownHandler(e)
    {
        cancelAnimationFrame(motion);
        motion = null;
         
        count = 0;
        timerCount = setInterval(countDown, 100);
        vy = 0;
        sy = 0;
        my = 0;
        mapTop = (canvas.currentStyle || document.defaultView.getComputedStyle(canvas,'')).top;
        mapTop = Number(mapTop.replace('px',''));
         
        if(g_mobiledevice)
        {
            var t = event.touches[0];
            sy = t.pageY;
        }
        else
        if(g_browsername == 'MSIE')
        {
            sy = event.y;
        }
        else
        if(g_browsername == 'Firefox')
        {
            sy = e.clientY;
        }
        else
        {
            sy = event.y + document.body.scrollTop;
        }
         
        vTotal = 0;
        canvas.addEventListener(buttonMove, buttonMoveHandler, false);
    }
     
    function buttonUpHandler(e)
    {
        if(count > inter)
        {
            vy = 0;
        }
        else
        {
            vy = my * 0.1;
        }
         
         
        slideAnimationY();
        canvas.addEventListener(buttonDown, buttonDownHandler, false);
        canvas.removeEventListener(buttonMove, buttonMoveHandler, false);
        clearInterval(timerCount);
        timerCount = null;
    }
     
    function animation()
    {
        var top = (canvas.currentStyle || document.defaultView.getComputedStyle(canvas,'')).top;
        top = Number(top.replace('px', ''));
         
        vy *= friction;
        var vTotal = top + vy;
         
        canvas.style.top = vTotal + "px";
         
        if(vTotal < topMax)
        {
            canvas.style.top = topMax + "px";
        }
        else
        if(vTotal >= 0)
        {
            canvas.style.top = 0 + "px";
        }
    }
     
    function buttonMoveHandler(e)
    {
        if(g_mobiledevice)
        {
            var t = event.touches[0];
            my = t.pageY - sy;
        }
        else
        if(g_browsername == 'MSIE')
        {
            my = event.y - sy;
        }
        else
        if(g_browsername == 'Firefox')
        {
            my = e.clientY + document.body.scrollTop - sy;
        }
        else
        {
            my = event.y + document.body.scrollTop - sy;
        }
        canvas.style.top = (mapTop + my) + 'px';
         
        var h = mapTop + my;
        if(h < topMax)
        {
            canvas.style.top = topMax + "px";
        }
        else
        if(h > 0)
        {
            canvas.style.top = 0 + "px";
        }
    }
     
    function mouseXY(e)
    {
        if(g_mobiledevice)
        {
            var rect = event.currentTarget.getBoundingClientRect();
            mouseX = event.touches[0].pageX - rect.left;
            mouseY = event.touches[0].pageY - rect.top;
        }
        else
        if(g_browsername == 'MSIE'||g_browsername == 'Opera')
        {
            rect = document.getElementById("canvas").getBoundingClientRect();
            mouseX = event.clientX - rect.left;
            mouseY = event.clientY - rect.top;
        }
        else
        if(g_browsername == 'Firefox')
        {
            rect = document.getElementById("canvas").getBoundingClientRect();
            mouseX = e.clientX - rect.left;
            mouseY = e.clientY - rect.top;
        }
        else
        {
            rect = event.currentTarget.getBoundingClientRect();
            mouseX = event.clientX - rect.left;
            mouseY = event.clientY - rect.top;
        }
    }
}
  
// 各ブラウザ対応
(function()
{
    var requestAnimationFrame = window.requestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.oRequestAnimationFrame ||
            window.msRequestAnimationFrame ||
            function(callback)
            {
                setTimeout(callback, 1000 / 60);
            };
 
    var cancelAnimationFrame = window.cancelAnimationFrame ||
            window.webkitCancelRequestAnimationFrame ||
            window.mozCancelRequestAnimationFrame ||
            window.oCancelRequestAnimationFrame ||
            window.msCancelRequestAnimationFrame ||
            function(id)
            {
                clearTimeout(id);
            };
 
    window.requestAnimationFrame = requestAnimationFrame;
    window.cancelAnimationFrame = cancelAnimationFrame;
})();

devicecheck.js

var g_mobiledevice = false; //iPhone、iPad、Androidのときtrue
var g_browsername = 'unknown';
var g_browserver = -1;
 
if(checkUserAgent() == false)
{
    window.alert('このブラウザは対象外です');
}
 
function checkUserAgent()
{
    var ua = navigator.userAgent;
 
    //iPhoneか?  
    var mstr = ua.match(/iPhone OS \d+/);
    if(mstr != null){
        var vstr = mstr[0].match(/\d+/);
        if(parseInt(vstr[0]) >= 3) 
        {
            g_mobiledevice = true;
            g_browsername = 'iPhone';
            //alert('major-version ' + vstr);
            return true;
        }
    }
    //iPadか?
    if(ua.indexOf('iPad') > -1)
    {
        mstr = ua.match(/CPU OS \d+/);
        if(mstr != null)
        {
            var vstr = mstr[0].match(/\d+/);
            if(parseInt(vstr[0]) >= 3) {
                g_mobiledevice = true;
                g_browsername = 'iPad';
                //alert('major-version ' + vstr);
                return true;
            }       
        }       
    }   
    //Androidか? 
    var mstr = ua.match(/Android \d+\.\d+/);
    if(mstr != null)
    {
        g_browsername = 'Android';
        var vstr = mstr[0].match(/\d+\.\d+/);
        g_browserver = parseFloat(vstr[0]);
        g_mobiledevice = true;
        if(pg_browserver > 2.1) 
        {
            //alert('version ' + vstr);
            return true;
        }
    }
    //Chromeか?
    mstr = ua.match(/Chrome\/\d+/);
    if(mstr != null)
    {
        g_browsername = 'Chrome';
        var vstr = mstr[0].match(/\d+/);
        g_browserver = parseInt(vstr[0]);
        if(g_browserver >= 9) 
        {
            //alert('major-version ' + vstr);
             
            return true;
        }       
    }
    //Safariか?
    if(ua.indexOf('Safari') > -1)
    {
        mstr = ua.match(/Version\/\d+/);
        if(mstr != null){
            var vstr = mstr[0].match(/\d+/);
            if(parseInt(vstr[0]) >= 5) 
            {
                g_browsername = 'Safari';
                //alert('major-version ' + vstr);
                return true;
            }       
        }       
    }
    //Internet Explorerか?
    mstr = ua.match(/MSIE \d+/);
    if(mstr != null)
    {
        var vstr = mstr[0].match(/\d+/);
        if(parseInt(vstr[0]) >= 9) 
        {
            g_browsername = 'MSIE';
            //alert('major-version ' + vstr);
            return true;
        }       
    }
    //Firefoxか?
    mstr = ua.match(/Firefox\/\d+/);
    if(mstr != null){
        var vstr = mstr[0].match(/\d+/);
        if(parseInt(vstr[0]) >= 4) 
        {
            g_browsername = 'Firefox';
            //alert('major-version ' + vstr);
            return true;
        }       
    }
    //Operaか?
    if(ua.indexOf('Opera') > -1)
    {
        mstr = ua.match(/Version\/\d+/);
        if(mstr != null){
            var vstr = mstr[0].match(/\d+/);
            if(parseInt(vstr[0]) >= 11) 
            {
                g_browsername = 'Opera';
                //alert('major-version ' + vstr);
                return true;
            }       
        }       
    }
     
    return false;
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です