Tuesday, March 11, 2014

scroll a full page height up or down with jQuery/Javascript


HTML :-

     <div class="div" style="text-align: center; color: green">
            Gallery</div>
        <div class="div" style="background-color: #aaa">
            Page 1</div>
        <div class="div" style="background-color: #444">
            Page 2</div>
        <div class="div" style="background-color: blue">
            Page 3</div>
        <div class="div" style="background-color: #ccc">
            Page 4</div>
        <div class="div" style="background-color: pink">
            Page 5</div>


Script : -

<script language="javascript" type="text/javascript">

        $(document).ready(function() {
            var divs = $('.div');
            divs.css('height',$( document ).height());
            var dir = 'up'; // wheel scroll direction
            var div = 0; // current div
            $(document.body).on('DOMMouseScroll mousewheel', function(e) {
                if (e.originalEvent.detail > 0 || e.originalEvent.wheelDelta < 0) {
                    dir = 'down';
                } else {
                    dir = 'up';
                }
                // find currently visible div :
                div = -1;
                divs.each(function(i) {
                    if (div < 0 && ($(this).offset().top >= $(window).scrollTop())) {
                        div = i;
                    }
                });
                if (dir == 'up' && div > 0) {
                    div--;
                }
                if (dir == 'down' && div < divs.length) {
                    div++;
                }
                //console.log(div, dir, divs.length);
                $('html,body').stop().animate({
                    scrollTop: divs.eq(div).offset().top
                }, 200);
                return false;
            });
            $(window).resize(function() {
                $('html,body').scrollTop(divs.eq(div).offset().top);
            });
        });
    </script>

Sunday, March 9, 2014

How to implement this cool popup div effect using jQuery?



HTML :-

<div class='container'>
    <button id="btnShow">Show</button>
    <div class='menu' style='display: none'>
        <button id="btnHide">Close</button><br/>
       ABCDEFGHI,<br/>
        ABCDEFGHI<br/>
        ABCDEFGHI <br/>
        ABCDEFGHI<br/>
        ABCDEFGHI<br/>
        ABCDEFGHI <br/>
        ABCDEFGHI <br/>
    </div>

</div>


JS : - 

  $(document).ready(function() {
            $('#btnShow').click(function() {
 $('.menu').show().css("top", "400px").animate({ top: 50 }, 200);
            });

            $('#btnHide').click(function() {
                $('.menu').hide();
            });
  });



CSS :- 

.container
        {
            with: 400px;
            height: 300px;
            border: 1px solid black;
        }
    .menu
        {
            position: absolute;
            border: 1px solid black;
            background: #fff;
            left: 180px;
        }    

Set a div to be fixed from the top for the rest of the scroll.



var fixed = false;
        $(document).scroll(function() {
            if ($(this).scrollTop() >= 220) {
                if (!fixed) {
                    fixed = true;
        $('div').css({ position: 'fixed', top: 20 });                }
            } else {
                if (fixed) {
                    fixed = false;
                    $('div').css({ position: 'static' });
                }
            }
        });