开发者

How can I add limits to a custom scrolling element?

I have a pretty huge image being displayed in a container, the image stretches with the view port as it gets resized, but as the image is so big I have added scroller buttons to the side of the page, up and down, the only problem I have now is that when I press up or down there is no limit, the user can keep going until the image is completely out of sight, how can I stop that from happening?

Here is the code I have thus far,

HTML:

<body>
    <div id="wrapper">  

        <div class="scroll top"></div> 

        <div id="content">

            <div id="zoom_container">

                <img id="image" src="8052x2000px" />

            </div>

        </div>

        <div class="scroll bot"></div>

    </div>

</body>

CSS:

body {
   width: 100%;
   height: 100%;
   margin: 0;
   padding: 0;
}

开发者_运维技巧#wrapper {
   overflow: hidden;
   height: 100%;
   max-height: 100%;
}

#content {
   min-height: 100% !important;
   height: 100%;
   width: 100%;
   margin: 0;
   padding: 0;
}

#image {
   position: fixed;
   top: 0;
   left: 0;
   width: 100%;
}

jQuery:

//side scroller bar
$('.scroll').live('click', function(){

    var direction = $(this).hasClass('top');
    var img_pos_top = $("#zoom_container img").position().top;
    var inc = 0;

    inc = $("#zoom_container img").height() / 10;

    if(direction)
    {

        inc = $("#zoom_container img").position().top + inc;

    }
        else
    {
        inc = $("#zoom_container img").position().top - inc;
    }

    $("#zoom_container img").css({ position: 'relative',top: inc });

});

so as you can see I am incrementing or decrementing the top positioning of the image by 10% of it's height each click, how can I make sure the top of the image will never go further down than the top of the viewport and the bottom of the image never further up than the bottom of the viewport?

Is there a better more efficient way of achieving the same result?


Have a try this one.

<html>
<head>
    <title>Canvas Sizing</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {

            var canvasContext;
            resizeCanvas();
            $(window).resize(function() { resizeCanvas() });

            function resizeCanvas()
            {
                var w = window.innerWidth - 40;
                var h = window.innerHeight - 40;
                var canvasString = '<canvas id="mainCanvas" width="' + w + '" height="' + h + '">Canvas is not supported</canvas>';             

                $('#contentholder').empty();
                $(canvasString).appendTo('#contentholder');
                canvasContext = $('#mainCanvas').get(0).getContext('2d');               

                drawOnCanvas();
            }

            function drawOnCanvas()
            {
                var x = 15;
                var y = 35;

                canvasContext.font = "30pt serif";
                canvasContext.fillStyle="#0f0";
                canvasContext.fillText("Hello World!", x, y);
            }
        });
        </script>
<style>
  #mainCanvas
  {
    background-color: #000;
    border: solid 3px #0F0;
  }
  body
  {
    background: #000;
  }
  #contentholder
  {
    width: 99%;
    height: 99%;
    margin: auto;
  }
 </style
</head>

<body>
    <div id="contentholder"></div>
</body>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜