博客
关于我
基于jquery的垂直滚动触发器,多参数可设置。
阅读量:449 次
发布时间:2019-03-06

本文共 3264 字,大约阅读时间需要 10 分钟。

最近闲下来,想着多写点实用的组件。后续会陆续放更多功能性模块,大家可以多关注一下。

首先,上一下组件的参数配置:

  • 类型(type):默认为"show",表示当浏览器滚动到指定位置的节点时触发"show"事件。"scroll"则表示当浏览器滚动经过指定节点时触发"scroll"事件。
  • 位置(pos):默认为"#scrollBox",用于获取指定节点,通过这个节点的位置来判断滚动情况。
  • 延迟距离(delayDistance):设置在指定位置的上下浮动距离,单位为像素,可为负值。
  • 单次触发(single):true表示只触发一次,false表示可以多次触发。
  • 进入回调(passCallback):当滚动超过指定位置时的触发函数。
  • 退出回调(backCallback):当滚动小于指定位置时的触发函数。

示例代码如下:

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/

你可能感兴趣的文章
Python启动脚本
查看>>
Python听写汇总结果
查看>>
python吊打Excel?屁!那是你不会用!
查看>>
Python合并两张图片 | 先叠透明度再合并 (附Demo)
查看>>
python各进制的表述与转换
查看>>
python各种库安装
查看>>
python可视化matplotlib_如何使用Python中最强大的可视化工具Matplotlib?
查看>>
python可维护性_使用这7大神器,让你的Python 代码更容易于维护
查看>>
python变量的详细教程_Python零基础入门教程之语法入门[变量](第二期)
查看>>
Python变量命名方法-ChatGPT4o作答
查看>>
Python变量与运算符
查看>>
Python变量/运算符/函数/模块/string
查看>>
python发送邮件的时候出现 error (535, b‘5.7.3 Authentication unsuccessful‘) 解决方法
查看>>
python发送notes邮件_使用python在Lotus Notes中发送邮件
查看>>
Python双版本下创建一个Scrapy(西瓜皮)项目
查看>>
Python双版本下No module named 'requests'
查看>>
python及pycharm2018软件安装教程
查看>>
python去重txt文本_Python实现的txt文件去重功能示例
查看>>
python去掉列表的逗号,从Python列表项中删除标点符号
查看>>
Python卸载所有包
查看>>