JQuery+DIV自定义滚动条样式的具体实现

时间:2024-04-22 13:27:07 

JQuery计算滚动条长度和位置,代码如下:

javascript


<script type="text/javascript">
    var scrMinHeight = 1; //滚动条最小高度
    var scrMaxHeight = 0; //滚动条最大高度
    var scrDefualtTop = 80; //滚动条默认位置
    var scrHeight = 0;
    //初始化滚动条
    function InitScroll() {
        scrMaxHeight = $("#mainScrollContent").height(); //文本框高度
        scrHeight = document.getElementById("mainScrollContent").scrollHeight; //滚动文本高度
        scrHeight = parseInt((scrMaxHeight / scrHeight) * scrMaxHeight);
        if (scrHeight <= scrMinHeight) { scrHeight = scrMinHeight; }
        if (scrHeight >= scrMaxHeight) { $("#scrollContent").hide(); }
        else {
            $("#scrollContent").show();
            $("#scrollContent .tiao_mid").css("height", (scrHeight - 19) + "px");
        }
    }

    $(document).ready(function () {
        $(".bod").height(($(document).height() - 80) + "px");
        $("#mainScrollContent").height(($(document).height() - 125) + "px");
        scrMaxHeight = ($(document).height() - 125); //滚动条最大高度
        $("#scrollBody").height(($(document).height() - 125) + "px");
        $("#scrollBodyBack").height(($(document).height() - 125) + "px");

        InitScroll();
        $("#mainScrollContent").scroll(function () {
            ChangeScroll();
        });
        var y1 = 0;
        $("#scrollContent").mousedown(function (event) {
            var scrContentTop = $("#scrollContent").css("top");
            y1 = event.clientY - parseInt(scrContentTop.replace("px", ""));
            $("#scrollContent").mousemove(function (event) {
                if ((event.clientY - y1) < scrDefualtTop) {
                    $("#scrollContent").css("top", scrDefualtTop + "px");
                }
                else if ((event.clientY - y1) > (scrDefualtTop + scrMaxHeight - scrHeight)) {
                    $("#scrollContent").css("top", (scrDefualtTop + scrMaxHeight - scrHeight) + "px");
                }
                else {
                    $("#scrollContent").css("top", (event.clientY - y1) + "px");
                }
                ChangeScrollContent();
            });
        }).mouseup(function () {
            $("#scrollContent").unbind("mousemove");
        }).mouseout(function () {
            $("#scrollContent").unbind("mousemove");
        });
    });

    //改变滚动内容位置
    function ChangeScrollContent() {
        var scrTop = $("#scrollContent").css("top");
        var st = parseInt(scrTop.replace("px", ""));
        st = ((st - scrDefualtTop) * document.getElementById("mainScrollContent").scrollHeight) / scrMaxHeight
        $("#mainScrollContent").scrollTop(st); //滚动的高度
    }

    //改变滚动条位置
    function ChangeScroll() {
        var scrTop = $("#mainScrollContent").scrollTop(); //滚动的高度
        scrTop = (scrTop * scrMaxHeight) / document.getElementById("mainScrollContent").scrollHeight + scrDefualtTop;
        $("#scrollContent").css("top", scrTop + "px");
    }
    </script>

滚动区域内容DIV:
html


<div class="jtc_neir" id="Div1" style="height: 100px;">
    营业总收入:11.66亿元(同比增长-1.75%)
    <br />
    <a href="bank.aspx">dddd</a>净利润:0.19亿元(同比增长23.72%)
    <br />
    每股收益:0.04元
    <br />
    净资产收益率:1.58%
    <br />
    毛利率:12.22%(同比增长39.89%)
    <br />
    总资产:30.46亿元(同比增长-7.14%)<br />
    as大苏打撒旦撒
    <br />
    阿斯蒂芬多个地方营业总收入:11.66亿元(同比增长-1.75%)
    <br />
    净利润:0.19亿元(同比增长23.72%)
    <br />
    每股收益:0.04元
    <br />
    净资产收益率:1.58%
    <br />
    毛利率:12.22%(同比增长39.89%)
    <br />
    总资产:30.46亿元(同比增长-7.14%)<br />
    as大苏打撒旦撒
    <br />
    阿斯蒂芬多个地方营业总收入:11.66亿元(同比增长-1.75%)
    <br />
    净利润:0.19亿元(同比增长23.72%)
    <br />
    每股收益:0.04元
    <br />
    净资产收益率:1.58%
    <br />
    毛利率:12.22%(同比增长39.89%)
    <br />
    总资产:30.46亿元(同比增长-7.14%)<br />
    as大苏打撒旦撒
    <br />
    阿斯蒂芬多个地方营业总收入:11.66亿元(同比增长-1.75%)
    <br />
    净利润:0.19亿元(同比增长23.72%)
    <br />
    每股收益:0.04元
    <br />
    净资产收益率:1.58%
    <br />
    毛利率:12.22%(同比增长39.89%)
    <br />
    总资产:30.46亿元(同比增长-7.14%)<br />
    as大苏打撒旦撒
    <br />
    阿斯蒂芬多个地方营业总收入:11.66亿元(同比增长-1.75%)
    <br />
    净利润:0.19亿元(同比增长23.72%)
    <br />
    每股收益:0.04元
    <br />
    净资产收益率:1.58%
    <br />
    毛利率:12.22%(同比增长39.89%)
    <br />
    总资产:30.46亿元(同比增长-7.14%)<br />
    as大苏打撒旦撒
    <br />
    阿斯蒂芬多个地方</div>
<div class="jtc_tiao" style="background-color: rgb(196,206,208); z-index: 2;" id="Div2">
</div>
<div class="jtc_tiao" style="background-color: rgb(222,222,222); z-index: 1; width: 16px;"
    id="Div3">
</div>
<div class="jtc_tiao" id="Div4">
    <div class="tiao_up">
    </div>
    <div class="tiao_mid">
    </div>
    <div class="tiao_bottom">
    </div>
</div>

主要样式:
css


.jtc_neir{margin-left:20px; margin-right:20px; color:#000000; font-size:12px; background:none; line-height:32px; overflow-y:scroll;overflow-x:hidden; 
          scrollbar-3dlight-color:rgb(222,222,222); 
    scrollbar-arrow-color:rgb(222,222,222); 
    scrollbar-base-color:rgb(222,222,222); 
    scrollbar-darkshadow-color:rgb(222,222,222); 
    scrollbar-face-color:rgb(222,222,222); 
    scrollbar-highlight-color:rgb(222,222,222); 
    scrollbar-shadow-color:rgb(222,222,222);} 

.jtc_tiao{width:8px; position:absolute; top:80px; right:20px; z-index:10;} 
.tiao_up{width:8px; height:10px; background:url(../images/scrollbar-3.jpg) left top no-repeat; line-height:0px; overflow:hidden;} 
.tiao_mid{width:8px; background:url(../images/scrollbar-3.jpg) -39px center  repeat-y; line-height:0px; overflow:hidden;} 
.tiao_bottom{width:8px; height:10px; background:url(../images/scrollbar-3.jpg) -13px bottom no-repeat;font-size:0; line-height:0px; overflow:hidden;} 

标签:JQuery,DIV,滚动条
0
投稿

猜你喜欢

  • Python3使用requests包抓取并保存网页源码的方法

    2022-05-27 06:48:38
  • Python游戏开发之Pygame使用的最全教程分享

    2021-01-11 05:45:55
  • 使用vuex缓存数据并优化自己的vuex-cache

    2024-04-30 10:46:33
  • Python使用PyAV提取视频关键帧的实践

    2023-04-29 23:45:04
  • Python&Matlab实现樱花的绘制

    2023-11-04 05:07:16
  • python实现QQ空间自动点赞功能

    2022-06-27 14:23:54
  • MySQL行级锁、表级锁、页级锁详细介绍

    2024-01-27 08:43:42
  • python生成tensorflow输入输出的图像格式的方法

    2021-03-20 05:21:08
  • asp ajax跨域提交数据

    2011-04-11 10:56:00
  • Python真题案例之错位键盘 单词长度 字母重排详解

    2023-03-18 02:57:26
  • 解决VueCil代理本地proxytable无效报错404的问题

    2024-05-11 09:13:33
  • 使用SQLSERVER 2005/2008 递归CTE查询树型结构的方法

    2024-01-27 15:13:10
  • Python定时器实例代码

    2021-11-18 23:03:37
  • 解决pycharm启动后总是不停的updating indices...indexing的问题

    2023-03-05 07:35:38
  • MySQL数据库的23个注意事项

    2024-01-23 11:26:06
  • 使用Microsoft SQL Server 2000全文搜索功能构建Web搜索应用程序

    2008-09-29 12:32:00
  • Oracle 分析函数RANK(),ROW_NUMBER(),LAG()等的使用方法

    2009-11-05 21:45:00
  • Python学习之加密模块使用详解

    2022-06-01 03:58:03
  • Django REST框架创建一个简单的Api实例讲解

    2023-04-28 01:02:40
  • oledb连接access数据库示例

    2024-01-28 05:54:19
  • asp之家 网络编程 m.aspxhome.com