Speed up development with full-stack environments for every branch.

Learn More

How to use jQuery's position().top function [DOM]

Forked from How to use the jQuery UI Slider.

648 Runs 4942 Views 875 Copies
Saved

Saved

pg1 45

pg1
published 4 years ago

<!doctype html>
<html>
<head>

    <title>How to use jQuery's position().top function</title>
    
    <style>
        body{
            text-align: center;
            font-family: arial;
        }
   
        .wrapper{
            text-align: left;
            width:200px;
            margin:0 auto;
        }
   
        .box{
            padding: 10px;
            background: orange;
            color:#fff;
            margin-bottom:10px;
        }

        .inner-content{
            background: blue;
            margin:10px;
            padding: 10px
        }

        .button{
            margin-top:20px;
            font-size:16px;
            font-weight: bold;
            padding:5px 10px;
        }
    </style>


</head>
<body>
    
    <div class="wrapper">
         <div class="box">
            I'm a box
            <div class="inner-content">this is inner content</div>
        </div>

        <input type="button" value="Get inner content top value" class="button" />
        <br />
        <br />

        <span id="results"></span>
    </div>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script>

        //When DOM loaded we attach click event to button
        $(document).ready(function() {
            $('.button').click(function(){

                //we select div and call position method and get top value
                var top = $('.inner-content').position().top;
                $('#results').html('The top position of the inner content is ' + top + 'px');

                //If you want the position relative to the document then use:
                //$(".inner-content").offset().top;

            });
            
        });

    </script>

</body>
</html>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).