zoukankan      html  css  js  c++  java
  • jquery scrollLeft居中

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="./base/jquery.js"></script>
        <style>
            ul, li {
                padding: 0px;
                margin: 0px;
                list-style: none;
            }
            ul {
                width: 1000000px;
            }
            li {
                height: 100px;
                float: left;
                border: 1px solid red;
            }
            li.selected {
                background-color: yellow;;
            }
        </style>
    </head>
    <body>
    <div class="container" id="J_container" style=" 200px;height: 100px;border:1px solid gray;     overflow-x: scroll;
        overflow-y: hidden;">
        <ul>
            <li>测试1</li>
            <li>测试1</li>
            <li>测试1</li>
            <li>测试1</li>
            <li>测试1</li>
            <li>测试1</li>
            <li>测试1</li>
            <li class="selected">请将我居中</li>
            <li>测试1</li>
            <li>测试1</li>
        </ul>
    </div>
    <script>
        $(function () {
            var item = $(".selected");
            var container = $("#J_container");
            var itemOffset = item.offset();
            var itemOffsetLeft = itemOffset.left + container.scrollLeft();
            var centerLeft = ( container.width() - item.width()) / 2;
            container.scrollLeft(itemOffsetLeft - centerLeft);
        })
    </script>
    </body>
    </html>

    技术交流QQ群:15129679

  • 相关阅读:
    exit()和_exit()函数(转)
    C语言struct内存占用问题 (转)
    C语言中memset函数(转)
    STDIN_FILENO与stdin区别(转)
    stderr,strerror,errno,perror,论坛大神的回答!
    C++ 函数模版
    C++ 内置函数
    offsetof
    LockSupportDemo
    读写锁,await和signa
  • 原文地址:https://www.cnblogs.com/yeminglong/p/6149105.html
Copyright © 2011-2022 走看看