侧边栏壁纸
  • 累计撰写 793 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

目 录CONTENT

文章目录

滚轮

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 177 阅读 / 238 字
温馨提示:
本文最后更新于 2022-07-23,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
mouseWheel: function () {
    function mousewheel(obj, upfun, downfun) {
        if (document.attachEvent) {
            obj.attachEvent("onmousewheel", scrollFn)
        } else {
            if (document.addEventListener) {
                obj.addEventListener("mousewheel", scrollFn, false);
                obj.addEventListener("DOMMouseScroll", scrollFn, false)
            }
        }

        function scrollFn(e) {
            var ev = e || window.event;
            var dir = ev.wheelDelta || ev.detail;
            if (ev.preventDefault) {
                ev.preventDefault()
            } else {
                ev.returnValue = false
            }
            if (dir == -3 || dir == 120) {
                upfun()
            } else {
                downfun()
            }
        }
    }

    var that = this;
    mousewheel($("." + that.bigimg)[0], function () {
        if ($("." + that.bigimg).innerWidth() > $("body").width() - 20) {
            return
        }
        if ($("." + that.bigimg).innerHeight() > $("body").height() - 50) {
            return
        }
        var zoomHeight = $("." + that.bigimg).innerHeight() * 1.03;
        var zoomWidth = $("." + that.bigimg).innerWidth() * 1.03;
        $("." + that.bigimg).css({height: zoomHeight + "px", width: zoomWidth + "px"})
    }, function () {
        if ($("." + that.bigimg).innerWidth() < 100) {
            return
        }
        if ($("." + that.bigimg).innerHeight() < 100) {
            return
        }
        var zoomHeight = $("." + that.bigimg).innerHeight() / 1.03;
        var zoomWidth = $("." + that.bigimg).innerWidth() / 1.03;
        $("." + that.bigimg).css({height: zoomHeight + "px", width: zoomWidth + "px"})
    })
}
检测滚动方向,闭包保证变量不污染外部
(function () {
    var previousScroll = 0;

    $(window).scroll(function(){
       var currentScroll = $(this).scrollTop();
       if (currentScroll > previousScroll){
           alert('down');
       } else {
          alert('up');
       }
       previousScroll = currentScroll;
    });
}()); //run this anonymous function immediately
0

评论区