如何使用jQuery滚动到水平div中的元素?

要滚动到水平div中的元素,请使用向左滚动键。

示例

您可以尝试运行以下代码以了解如何滚动到jQuery中的元素:

<html>
   <head>
   
      <title>jQuery Scroll</title>
      <script src = "https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
       
      <script>
         $(document).ready(function(){
           document.addEventListener('DOMContentLoaded', function () {
              var frames = document.getElementById('frames');
              frames.addEventListener('click', function (e) {
                 if (e.target.classList.contains('item')) {
                   e.target.parentNode.scrollLeft = e.target.offsetLeft;
                 }
             });
          });
               
         });
      </script>
       
      <style>
     
        .myclass {
            width: 300px;
            display: flex;
            position: relative;
            overflow-x: scroll;
        }
   
        .item {
            width: 400px;
            background: #FFB563;
            margin-right: 40px;
        }
         </style>
         </head>
   <body>
   <div class="myclass" id="frames">
        <div class="item frames-item">demo1</div>
        <div class="item frames-item">demo2</div>
        <div class="item frames-item">demo3</div>
        <div class="item frames-item">demo4</div>
        <div class="item frames-item">demo5</div>
        <div class="item frames-item">demo6</div>
        <div class="item frames-item">demo7</div>
        <div class="item frames-item">demo8</div>
    </div>
   </body>
   
</html>