本文共 3337 字,大约阅读时间需要 11 分钟。
最近闲下来,想着多写点实用的组件。后续会陆续放更多功能性模块,大家可以多关注一下。
首先,上一下组件的参数配置:
示例代码如下:
function scrollTrigger(obj) { this.set = { type: "show", pos: "#scrollBox", delayDistance: 0, single: true, passCallback: function() {}, backCallback: function() {} }; this.passFlag = false; this.backFlag = false; $.extend(this.set, obj); var _this = this; this.init = function() { $(window).scroll(function() { if (_this.set.type === "scroll") { if ($(window).scrollTop() > $($this.set.pos).offset().top + _this.set.delayDistance) { if (_this.set.single === true && _this.passFlag === false) { _this.set.passCallback(); _this.passFlag = true; } else if (_this.set.single === true && _this.passFlag === true) { // 无操作 } else { _this.set.passCallback(); } } if ($(window).scrollTop() < $($this.set.pos).offset().top + _this.set.delayDistance) { if (_this.set.single === true && _this.backFlag === false) { _this.set.backCallback(); _this.backFlag = true; } else if (_this.set.single === true && _this.backFlag === true) { // 无操作 } else { _this.set.backCallback(); } } } if (_this.set.type === "show") { if ($(window).scrollTop() > $($this.set.pos).offset().top - $(window).height() + _this.set.delayDistance) { if (_this.set.single === true && _this.passFlag === false) { _this.set.passCallback(); _this.passFlag = true; } else if (_this.set.single === true && _this.passFlag === true) { // 无操作 } else { _this.set.passCallback(); } } if ($(window).scrollTop() + $(window).height() < $($this.set.pos).offset().top + _this.set.delayDistance) { if (_this.set.single === true && _this.backFlag === false) { _this.set.backCallback(); _this.backFlag = true; } else if (_this.set.single === true && _this.backFlag === true) { // 无操作 } else { _this.set.backCallback(); } } } }); }; this.init();} 调用方法如下:
var trigger1 = new scrollTrigger({ type: "show", pos: "#trigger1", single: false, delayDistance: 0, passCallback: function() { console.log("pass"); }, backCallback: function() { console.log("back"); }}); 这个组件主要用于监听页面滚动事件,根据配置的条件自动触发指定的回调函数,适用于各种滚动控制场景。
转载地址:http://wiufz.baihongyu.com/