Open In App

Fabric.js getScrollLeftTop() Method

The getScrollLeftTop() method in Fabric.js is used to find the scroll offsets of an element. The object returned by this method contains the “top” and “left” property that denotes the “top” and “left” offsets respectively. The HTML element in the page has to be selected first to pass this method.

Syntax:



getScrollLeftTop(element)

Parameters: This method accepts a single parameter as mentioned above and described below:

Return Value: This method returns an object that contains the “top” and “left” scroll offsets as properties.



The below example demonstrates the getScrollLeftTop() method Fabric.js:

Example: 




<html>
  
<head>
    <!-- Adding the Fabric.js library -->
    <script src=
    </script>
  
    <style>
      
        /* Define the CSS classes to be used */
        .box1 {
            margin: 25px;
            width: 400px;
            height: 400px;
            background-color: red;
        }
  
        .box2 {
            margin: 25px;
            width: 700px;
            height: 300px;
            background-color: green;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <h3>
        Fabric.js util.getScrollLeftTop() Method
    </h3>
      
    <div class="box1">
        Element 1
    </div>
      
    <div class="box2">
        Element 2
    </div>
  
    <script>
  
        // Select the elements to be used
        let elem =
            document.querySelector('.box1');
        let elem2 =
            document.querySelector('.box2');
  
        function getScrollOffsets() {
            console.clear();
            console.log(
                "Current Left and Top Offsets are:"
            );
  
            // Find the scrolled left and top offsets
            console.log(
                fabric.util.getScrollLeftTop(elem)
            );
            console.log(
                fabric.util.getScrollLeftTop(elem2)
            );
        }
  
        // Repeatedly call the function
        // to observe the values
        setInterval(getScrollOffsets, 1000);
    </script>
</body>
  
</html>

Output:


Article Tags :