如何检测滚动位置以执行 jQuery

How to detect scroll position to execute jQuery

本文关键字:位置 执行 jQuery 滚动 何检测 检测      更新时间:2023-09-26

Current Code(但不像看起来那样运行)。

$(window).scroll(function() {
      if ($(this).scrollTop() > 1100) {

问题。

上面看起来它应该可以工作; 但并没有像您想象的那样读取滚动位置,而是在触摸任何滚动位置时触发。因此,当用户滚动时,它会加载(我的图表.js动画) - 每次在主滚动条调整后刷新动画最轻微。

要求。

我正在寻求测量页面顶部或底部的高度以检测滚动位置,甚至使用锚链接>一旦达到指定的滚动位置,动画就会加载一次并在动画完成时保持原样。我看过很多关于此的帖子,但没有真正的解决方案。

最近更新。

我已经尝试了这两种建议;在这些建议的调用中使用我的图表.js动画脚本,但出现了相同的问题 - 动画在每次触摸滚动条时刷新和查询。

var breakpoint = false;
$(window).scroll(function() {
  if ($(this).scrollTop() > 400 && !breakpoint ) {
    // do stuff
    // $(window).scroll(function() {
    //   if ($(this).scrollTop() > 1100) {
        // $(function () {
       //  $(document).ready(function () {
        var chart = null;
        var colors = Highcharts.getOptions().colors,
            categories = ['Home', 'Own', 'Shop', 'Buy'],
            name = 'Browser brands',
            data = [{
            //     y: 55.11,
            //     color: colors[0],
            //     drilldown: {
            //         name: 'MSIE versions',
            //         categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],
            //         data: [10.85, 7.35, 33.06, 2.81],
            //         color: colors[0]
            //     }
            // }, {
                y: 21.63,
                color: colors[1],
                drilldown: {
                    name: 'Buy',
                    // categories: ['Firefox 2.0', 'Firefox 3.0', 'Firefox 3.5', 'Firefox 3.6', 'Firefox 4.0'],
                    data: [0.20, 0.83, 1.58, 13.12, 5.43],
                    color: colors[1]
                }
            }, {
                y: 11.94,
                color: colors[2],
                drilldown: {
                    name: 'Shop',
                    // categories: ['Chrome 5.0', 'Chrome 6.0', 'Chrome 7.0', 'Chrome 8.0', 'Chrome 9.0',
                        // 'Chrome 10.0', 'Chrome 11.0', 'Chrome 12.0'],
                    data: [0.12, 0.19, 0.12, 0.36, 0.32, 9.91, 0.50, 0.22],
                    color: colors[2]
                }
            }, {
                y: 7.15,
                color: colors[3],
                drilldown: {
                    name: 'Own',
                    // categories: ['Safari 5.0', 'Safari 4.0', 'Safari Win 5.0', 'Safari 4.1', 'Safari/Maxthon',
                        // 'Safari 3.1', 'Safari 4.1'],
                    data: [4.55, 1.42, 0.23, 0.21, 0.20, 0.19, 0.14],
                    color: colors[3]
                }
            }, {
                y: 2.14,
                color: colors[4],
                drilldown: {
                    name: 'Home',
                    // categories: ['Opera 9.x', 'Opera 10.x', 'Opera 11.x'],
                    data: [0.12, 0.37, 1.65],
                    color: colors[4]
                }
            }];

        // Build the data array
        var browserData = [];
        for (var i = 0; i < data.length; i++) {
            // add browser data
            browserData.push({
                name: categories[i],
                y: data[i].y,
                color: data[i].color
            });
        }
        // Create the chart
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'pie'
            },
            title: {
                text: null
            },
            series: [{
                name: 'Hot Links',
                data: browserData,
                innerSize: '30%'
            }],
            tooltip: {
                valueSuffix: '%',
                positioner: function () {
                    return {
                        x: this.chart.series[0].center[0] - (this.label.width / 2) + 8,
                        y: this.chart.series[0].center[1] - (this.label.height / 2) + 8
                    };
                }
            },
            plotOptions: {
                pie: {
                    cursor: 'pointer',
                    dataLabels: {
                        connectorColor: 'white'
                    },
                    point: {
                        events: {
                            mouseOver: function () {
                                if (!this.connector.dynamicConnector) {
                                    var width = 16,
                                        height = 24;
                                    // Extract the connector start position
                                    var dArr = this.connector.d.split(' ');
                                    var startY = dArr.pop(),
                                        startX = dArr.pop();
                                    var start = [parseFloat(startX), parseFloat(startY)];
                                    // Construct the triangle
                                    var path = 'M ' + start[0] + ' ' + start[1] + 'L ' + (start[0] + height) + ' ' + (start[1] - (width / 2)) + ' L ' + (start[0] + height) + ' ' + (start[1] + (width / 2)) + ' L ' + start[0] + ' ' + start[1];
                                    // Convert the section angle from radian to degree and apply to the trangle
                                    // Setup rotation, x, y required by the updateTransform method
                                    this.connector.rotation = (this.angle * 180) / Math.PI;
                                    this.connector.x = startX;
                                    this.connector.y = startY;
                                    this.connector.updateTransform();
                                    this.connector.attr('stroke', this.color);
                                    this.connector.attr('fill', Highcharts.Color(this.color).brighten(0.2).get());
                                    this.connector.attr('d', path);
                                    this.connector.dynamicConnector = true;
                                }
                                this.connector.show();
                            },
                            mouseOut: function () {
                                this.connector.hide();
                            }
                        }
                    }
                }
            }
        });
//     });
// });
    }
});

使用更高范围的布尔值控制它

var breakpoint = false;
$(window).scroll(function() {
  if ($(this).scrollTop() > 400 && !breakpoint ) {
     doStuff();
  }
  if ($(this).scrollTop() < 400 && breakpoint ) {
     doOtherStuff();
  }
})
function doStuff() {
  breakpoint = true;
  console.log('we passed the breakpoint, do stuff');
}
function doOtherStuff() {
  breakpoint = false;
  console.log('were above the breakpoint, do other stuff');
}

编辑 添加了反向功能,用于返回断点上方。

最简单的解决方案是在条件发生后删除事件侦听器:

var handler = function() {
  if ($(this).scrollTop() > 1100) {
     // Do stuff
     $(window).off('scroll',handler);
  }
}
$(window).on('scroll',handler);