在JQ上定义滚动条插件

$.fn.definedScroll = function(direction, childId, scrollId) {
var parent = $(this),
child = parent.find(childId),
scroll = parent.find(scrollId);

var childDiffHeight = child.height() - parent.height(),
scrollDiffHeight = parent.height() - scroll.height(),
childDiffWidth = child.width() - parent.width(),
scrollDiffWidth = parent.width() - scroll.width();

if (direction === "y") {
if (childDiffHeight < 0 || scrollDiffHeight < 0) {
return false
}
}
if (direction === "x") {
if (childDiffWidth < 0 || scrollDiffWidth < 0) {
return false
}
}
scroll.on("mousedown", function(ev) {
ev.preventDefault();
var initTop = ev.clientY,
initLeft = ev.clientX,
top = parseInt(scroll.css("top")),
left = parseInt(scroll.css("left"));

$(document).bind("mousemove",
function(ev) {
if (direction === "y") {
var diffT = ev.clientY - initTop + top,
scrollDiffPercent = diffT / scrollDiffHeight,
childMoveT = -scrollDiffPercent * childDiffHeight;
if (diffT < 0) {
scroll.css("top", 0);
child.css("margin-top", 0)
} else if (diffT > scrollDiffHeight) {
scroll.css("top", scrollDiffHeight);
child.css("margin-top", -childDiffHeight)
} else {
scroll.css("top", diffT);
child.css("margin-top", childMoveT)
}
} else if( direction === "x" ) {
var diffL = ev.clientX - initLeft + left,
scrollDiffPercent = diffL / scrollDiffWidth,
childMoveL = -scrollDiffPercent * childDiffWidth;
if (diffL < 0) {
scroll.css("left", 0);
child.css("margin-left", 0)
} else if (diffL > scrollDiffWidth) {
scroll.css("left", scrollDiffWidth);
child.css("margin-left", -childDiffWidth)
} else {
scroll.css("left", diffL);
child.css("margin-left", childMoveL);
}
}
});

$(document).bind("mouseup", function() {
$(document).unbind("mousemove")
});
});

parent.get(0).addEventListener("DOMMouseScroll", handleMouseWheel, false);
parent.get(0).addEventListener("mousewheel", handleMouseWheel, false);

function handleMouseWheel(ev) {
ev.preventDefault();
var scale = Math.max( - 1, Math.min(1, (ev.wheelDelta || -ev.detail))),
wheelHeight = scale * parent.height() * 0.618,
scrollTop = parseInt(scroll.css("top"));

if( scrollTop - wheelHeight < 0 ) {
scroll.css( "top", 0 );
child.css( "margin-top", 0 );
} else if(scrollTop - wheelHeight > scrollDiffHeight) {
scroll.css( "top", scrollDiffHeight );
child.css( "margin-top", -childDiffHeight );
$(document).unbind('mousemove');
} else {
var diffPercen = ( scrollTop - wheelHeight ) / scrollDiffHeight;
var childMove = - diffPercen * childDiffHeight;
scroll.css( "top", scrollTop - wheelHeight );
child.css( "margin-top", childMove );
}
}
return this
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当鼠标在页面上按下时,可以使用jQuery中的鼠标按下事件(mousedown event)来监听。一旦鼠标按下,就可以执行代码来实现自动滚动条流动到当前位置。 首先,我们需要获取当前滚动条的位置。可以使用jQuery的scrollTop()方法来获取垂直滚动条的位置,或者使用scrollLeft()方法获取水平滚动条的位置。 接下来,我们可以使用animate()方法来创建动画效果,使滚动条平滑流动到当前位置。在animate()方法中,我们可以设置scrollTop或scrollLeft属性的值为当前滚动条的位置,从而实现流动效果。 最后,我们将这段代码绑定在鼠标按下事件的回调函数中,以便在鼠标按下时执行。具体代码如下: ```javascript $(document).mousedown(function() { var currentScrollTop = $(window).scrollTop(); // 获取当前垂直滚动条位置 $('html, body').animate({ scrollTop: currentScrollTop }, 500); // 将滚动条流动到当前位置,动画持续时间为500毫秒 }); ``` 在上述代码中,我们通过监听整个文档的鼠标按下事件来实现滚动条的自动流动。当鼠标按下时,获取当前滚动条位置,并使用animate()方法创建动画效果,将滚动条平滑流动到当前位置。 请注意,上述代码假设你使用的是最新版本的jQuery,并且已经引入了jQuery库。如果需要适配旧版本的jQuery或者其他JavaScript库,请根据实际情况进行修改和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值