jQuery a+Scroll 自定义滚动条代码

依赖插件
jquery.min.js
jquery.mousewheel.min.js
jquery.resize.min.js

<div class="a-scroll-container">
	<div class="a-scroll-wrapper">
		<div class="a-scroll-view">
			在这里插入内容
		</div>
	</div>
</div>
.a-scroll-container {
	position: relative;
}
.a-scroll-wrapper::before,
.a-scroll-wrapper::after {
	content: ' ';
	display: table;
}
.a-scroll-wrapper::after{
	clear: both;
}
.a-scroll-view {
	-webkit-transition: all .3s ease-out;
	transition: all .3s ease-out;
}
.a-scroll-drag .a-scroll-view {
	-webkit-transition: none;
	transition: none;
}
.a-scroll-bar {
	position: absolute;
	right: 2px;
	bottom: 2px;
	border-radius: 4px;
	-webkit-transition: all .12s ease-out;
	transition: all .12s ease-out;
	opacity: 0;
	z-index: 1;
}
.a-scroll-bar.a-scroll-vertical {
	width: 6px;
	top: 2px;
}
.a-scroll-container:hover .a-scroll-bar {
	opacity: 1;
}
.a-scroll-bar-thumb {
	display: block;
	position: relative;
	width: 0;
	height: 0;
	background-color: rgba(144, 147, 153, .3);
	border-radius: inherit;
	-webkit-transition: all .3s ease-out;
	transition: all .3s ease-out;
	-webkit-user-select: none;
	user-select: none;
	cursor: pointer;
}
.a-scroll-bar.a-scroll-vertical .a-scroll-bar-thumb {
	width: 100%;
}
.a-scroll-bar-thumb:hover,
.a-scroll-bar-thumb:active {
	background-color: rgba(144, 147, 153, .5);
}
.a-scroll-drag .a-scroll-bar-thumb {
	-webkit-transition: none;
	transition: none;
}
jQuery.extend({
	aScroll: function() {
        $('.a-scroll-container').each(function() {
            var _this = $(this);

    		if (_this.attr('data-start') == undefined) {
    			_this.attr('data-start', 'true');
    
    			if (_this.find('.a-scroll-vertical').length == 0) {
    				_this.append('\
    					<div class="a-scroll-bar a-scroll-vertical">\
    						<div class="a-scroll-bar-thumb"></div>\
    					</div>\
    				');
    			}
    		}
    
    		var _wrapper = _this.find('.a-scroll-wrapper'),
    			_view = _this.find('.a-scroll-view'),
    			_scroll = _this.find('.a-scroll-bar'),
    			_thumb = _scroll.find('.a-scroll-bar-thumb');
    
    		_view.on('resize', function () {
    			aScrollInit();
    		})
    
            $(window).resize(function() {
                aScrollInit();
            });
    
    		aScrollInit();
    
    		function aScrollInit () {
    			_wrapper.off('mousewheel');
    			_scroll.off('mousedown touchstart');
    			_thumb.off('mousedown touchstart');
    			$(document).off('mousemove touchmove');
    			$(document).off('mouseup touchend');
    
    			_thumb.removeAttr('style');
    			_view.removeAttr('style');
    
    			if (_view.outerHeight(true) > _wrapper.outerHeight(true)) {
    				var isDrag = false,
    					rate = _this.outerHeight(true) / _view.outerHeight(true),
    					wheel = -(_view.position().top),
    					pageY = 0,
    					viewT = 0,
    					barT = 0,
    					barH = Math.round(rate *  _scroll.outerHeight(true)),
    					wheelMax = _view.outerHeight(true) - _wrapper.outerHeight(true),
    					barMax = _scroll.outerHeight(true) - barH;
    
    				_thumb.css('height', barH + 'px');
    
    				_wrapper.on('mousewheel', function (event, direction) {
    					if (isDrag == false) {
    						if (direction > 0) {
    							wheel -= 25;
    						} else if (direction < 0) {
    							wheel += 25;
    						}
    
    						wheel = wheel < 0 ? 0 : (wheel > wheelMax ? wheelMax : wheel);
    
    						_view.css('transform', 'translateY(' + -wheel + 'px)');
    						_thumb.css('transform', 'translateY(' + Math.round(wheel / _view.outerHeight(true) * _scroll.outerHeight(true)) + 'px)');
    					}
    				})
    				_scroll.on('mousedown touchstart', function (event) {
    					var scrollT = $(this).offset().top,
    						b = event.pageY - scrollT - (barH / 2),
    						w = _view.outerHeight(true) * (b / _scroll.outerHeight(true));
    
    					b = b < 0 ? 0 : (b > barMax ? barMax : b);
    					w = w < 0 ? 0 : (w > wheelMax ? wheelMax : w);
    
    					wheel =  w;
    
    					_thumb.css('transform', 'translateY(' + b + 'px)');
    					_view.css('transform', 'translateY(' + -w + 'px)');
    
    					return false;
    				})
    				_thumb.on('mousedown touchstart', function (event) {
    					isDrag = true
    					barT = _thumb.position().top;
    					viewT = _view.position().top;
    					pageY = event.pageY;
    
    					_this.addClass('a-scroll-drag');
    
    					return false;
    				})
    				$(document).on('mousemove touchmove', function (event) {
    					if (isDrag) {
    						var r = event.pageY - pageY,
    							w = -viewT + (_view.outerHeight(true) * (r / _scroll.outerHeight(true))),
    							b = barT + r;
    
    						w = w < 0 ? 0 : (w > wheelMax ? wheelMax : w);
    						b = b < 0 ? 0 : (b > barMax ? barMax : b);
    
    						wheel =  w;
    
    						_view.css('transform', 'translateY(' + -w + 'px)');
    						_thumb.css('transform', 'translateY(' + b + 'px)');
    					}
    				})
    				$(document).on('mouseup touchend', function (event) {
    					isDrag = false;
    
    					_this.removeClass('a-scroll-drag');
    				})
    			}
    		}
        })
	}
})
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A哎呀妈呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值