sexta-feira, 8 de março de 2019

ajax carregar quando atingir o ecran

<!DOCTYPE html>

<html>

<head>
    <style>
        #element {
            margin: 140px;
            text-align: center;
            padding: 5px;
            width: 200px;
            height: 200px;
            border: 1px solid #0099f9;
            border-radius: 3px;
            background: #444;
            color: #0099d9;
            opacity: 0.6;
        }

        #log {
            position: fixed;
            top: 40px;
            left: 40px;
            color: #333;
        }

        #scroll {
            position: fixed;
            bottom: 10px;
            right: 10px;
            border: 1px solid #000;
            border-radius: 2px;
            padding: 5px;
        }

    </style>
</head>

<body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="log"></div>

    <div id="element">Hello
        <hr>World</div>
    <div id="scroll">Scroll Down</div>
    <script type="text/javascript">
        function log(txt) {
           if (txt<10){
                alert("Conseguiste");}
           $("#log").html("location : <b>" + txt + "</b> px");
           
        }

        $(function() {
            var eTop = $('#element').offset().top; //get the offset top of the element
            log(eTop - $(window).scrollTop()); //position of the ele w.r.t window
           

            $(window).scroll(function() { //when window is scrolled
                log(eTop - $(window).scrollTop());
            });
        });

    </script>
</body>

</html>

Sem comentários:

Enviar um comentário