您当前位置:首页 > 文章中心 > SCSCMS

scroll模拟滚动条

稿件来源: 阳光企业网站管理系统   撰稿作者: 太阳光   发表日期: 2015-1-28   阅读次数: 16   查看权限: 游客查看

scroll模拟滚动条

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>scroll模拟滚动条</title>
    <script type="text/javascript" src="images/jquery-1.4.4.min.js"></script>
    <style type="text/css">
        *{margin: 0;padding: 0;list-style: none}
        #tt{margin:50px;}
        .scs_scroll{overflow: hidden;position: relative;border: 1px solid #eee}
        .scs_scroll_bg,.scs_scroll_bar{position: absolute;right: 0;top:0;width: 10px;background-color: #f2f2f2;z-index: 100}
        .scs_scroll_bar{background-color: #bcbcbc;border-radius: 5px;height: 100px;z-index: 101;cursor: pointer}
        ul{background-color: #f9f9f9;position: absolute;width: 100%}
        ul li{border-bottom: 1px solid #ddd}
    </style>
</head>
<body>
<div id="tt">
    <div class="scs_scroll" style="height:200px;width:200px">
        <ul>
            <li>1</li>
            <li>2</li>
        </ul>
    </div>
</div>
<button type="button">增高</button>
<button type="button">减高</button>
<script type="text/javascript">
(function($) {
    $.extend($.fn, {
        scs_scroll: function() {
            var _this = $(this);
            _this.each(function(){
                var _self = $(this),i_outH = _self.height(),i_inH = _self.children(":first").height(),
                        i_percentage = i_outH / i_inH,_data = _self.data("scroll"),_move = false,_y;
                if(i_percentage < 1){
                    if("undefined" == typeof _data){
                        _data = {div:_self.children().css("position","absolute"),outHeight:i_outH,inHieght:i_inH};
                        _data.bg = $('<div class="scs_scroll_bg" style="height: '+ i_outH +'px"></div>').appendTo(_self)
                                .mousedown(function(e){
                                    _data = $(this.parentNode).data("scroll");
                                    _y = parseInt(e.pageY - $(this).offset().top - _data.bar.height() / 2);
                                    _y < 0 && (_y = 0);
                                    _y > _data.max && (_y = _data.max);
                                    _data.bar.css({top:_y});
                                    _data.div.css({top:-1*_y /_data.max * (_data.inHieght - _data.outHeight)});
                                }).hover(function(){$(this).css("background-color","#e9e9e9")},function(){$(this).css("background-color","#f2f2f2")});
                        _data.bar = $('<div class="scs_scroll_bar" style="height:'+i_percentage * i_outH+'px"></div>').appendTo(_self)
                                .mousedown(function (e) {
                                    _data = $(this.parentNode).data("scroll");
                                    _move = true;
                                    _y = e.pageY - parseInt($(this).css("top"));
                                }).mousemove(function (e) {
                                    var y = e.pageY - _y;
                                    if (_move && y >= 0 && y <= _data.max){
                                        $(this).css({top:y});
                                        //滚动效果
                                        _data.div.css({top:-1*y /_data.max * (_data.inHieght - _data.outHeight)});
                                    }
                                }).mouseup(function () {
                                    _move = false;
                                }).mouseleave(function () {
                                    _move = false;
                                });
                        _data.max = (1 - i_percentage) * i_outH;
                        _self.data("scroll",_data);
                        function scrollFunc(e){
                            if(_data.bar.is(":visible")){
                                e = e || window.event;
                                var direct = e.wheelDelta || e.detail || 0,t = parseInt(_data.div.css("top")) || 0,max = _data.outHeight - _data.inHieght;
                                t += 30 * (direct < 0 ? 1 : -1);
                                t < max && (t = max);
                                t > 0 && (t = 0);
                                _data.div.css("top",t);
                                _data.bar.css("top",t/max * _data.max);
                                e.preventDefault && e.preventDefault();//阻止滚动事件冒泡
                                e.cancelBubble = true;//IE阻止滚动事件冒泡
                            }
                            return false;//IE低版本阻止滚动事件冒泡
                        }
                        document.addEventListener && this.addEventListener("DOMMouseScroll",scrollFunc, false);
                        this.onmousewheel = scrollFunc;
                    }
                    if(i_inH != _data.inHieght || i_outH != _data.outHeight){
                        _data.max = (1 - i_percentage) * i_outH;
                        _y = parseInt(_data.bar.css("top"),10);
                        if(_y > _data.max){
                            _y = _data.max;
                            _data.div.css({top:-1*_y /_data.max * (i_inH - i_outH)});
                        }
                        _data.bar.css({top:_y,height:i_percentage * i_outH});
                        _data.inHieght = i_inH;
                        _data.outHeight = i_outH;
                        _self.data("scroll",_data);
                        //可能露底了
                        if(i_inH < i_outH + Math.abs(_data.div.position().top)){
                            _data.bar.css({top:_data.max});
                            _data.div.css({top:i_outH - i_inH});
                        }
                    }
                }else if("object" == typeof _data){
                    _data.bg.remove();
                    _data.bar.remove();
                    _self.children(":first").css({top:0});
                    _self.removeData("scroll");//直接删除元素并移除data
                }
            });
            setTimeout(function(){_this.scs_scroll()},200);
            return _this;
        }
    });
})(jQuery);

$(function () {
    $(".scs_scroll").scs_scroll();
    var i = 1;
    $("button").click(function(){
        $(this).text() == "增高" ? $("ul").prepend('<li>'+(++i)+'</li>') : $("ul li:first").remove();
    })
});
</script>
</body>
</html>

 

关键词: jquery插件,滚动条   编辑时间: 2015-1-28 21:26:50

  • 感到高兴

    0

    高兴
  • 感到支持

    0

    支持
  • 感到搞笑

    0

    搞笑
  • 感到不解

    0

    不解
  • 感到谎言

    0

    谎言
  • 感到枪稿

    0

    枪稿
  • 感到震惊

    0

    震惊
  • 感到无奈

    0

    无奈
  • 感到无聊

    0

    无聊
  • 感到反对

    0

    反对
  • 感到愤怒

    0

    愤怒
0%(0)
0%(0)
上一篇:html5锁定元素
下一篇:js阳历转阴历
共有0 条评论 发言请遵守【相关规定

网友评论

会员头像
发 表同步腾讯微博  匿名评论  验证码:  点击更新
  • 暂无评论
关闭模块文章图片 article Pictrue
  • 代码覆盖率工具 Istanbul 入门教程
  • 全栈工程师的武器——MEAN
  • 9款超炫的 CSS3 复选框(Checkbox)
  • 微信开发在线翻译功能
  • CSS3那些不为人知的高级属性
  • 给easyui的datebox添加清空事件
  • flash写字效果
  • kendoUI系列教程之DropDownList下拉菜单
  • kendoUI系列教程之datetimepicker日期时间选择
  • kendoUI系列教程之datepicker日期选择
  • kendoUI系列教程之combobox下拉列表框
  • kendoUI系列教程之colorpicker
  • kendoUI系列教程之calendar日历表
  • kendoUI系列教程之autocomplete自动补齐
  • kendo ui简介
  • QQ登录网站实战教程