JavaScript 的触发点取决于视口大小

发布于 2024-10-20 23:05:59 字数 3089 浏览 0 评论 0原文

因此,我正在编写一个网站,当您向下滚动页面时,该网站会触发某些事件。我希望当相关元素到达视口下方四分之一左右的点时触发事件。

然而,对于不同大小的视口,这个触发点显然是不同的。我已经弄清楚如何计算这个触发点,但我还没有找到一种方法来获取 div 相对于视口/页面顶部的位置。

我正在尝试使用 .offset() ,我可以将其与 getPageScroll() 结合起来找到正确的点,但我不知道到底该如何处理它返回的数组。我还尝试将其弹出到变量中,并使用下面的语法(如 jquery.com 文档中使用的那样),但它显然是错误的,并在控制台中返回 Undefined 。

我对 Javascript 和 jQuery 以及任何实际编程都很陌生,所以请原谅任何愚蠢的行为。如果我把这一切倒过来,这也是一个完全有效的答案!如果是这样的话,请给我指出正确的方向。

到目前为止我已经像这样编码了。目前实际效果只是占位符 - 我只是想让基本框架正常工作:

// getPageScroll() by quirksmode.com - adapted to only return yScroll
function getPageScroll() {
    var yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
      yScroll = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
    }
    return yScroll
}

// Adapted from getPageSize() by quirksmode.com
function getPageHeight() {
    var windowHeight
    if (self.innerHeight) { // all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }
    return windowHeight
}



var containers = $('div.container');
var element_1 = $('#part_one');
var element_2 = $('#part_two_1');
var element_3 = $('#part_two_2');
var element_4 = $('#part_two_3');
var element_5 = $('#part_two_4');
var element_6 = $('#part_three');
var element_7 = $('#part_four');
var element_8 = $('#part_five');

var docHeight = $(document).height();

$(window).scroll(function() {

    var offset = offset();
    var docHeight = $(document).height();

    var pageBottom = getPageScroll() + getPageHeight(); 

    var quarterPoint = getPageScroll()+((pageBottom-getPageScroll())/4)
    var halfwayPoint = getPageScroll()+((pageBottom-getPageScroll())/2)
    var threeQuarterPoint = pageBottom-((pageBottom-getPageScroll())/4)
    var triggerPoint = quarterPoint-(getPageHeight/10)

    if (triggerPoint < element_1.offset.top){

        containers.stop().animate({backgroundColor: "white", color: "#aaa"}, 50);
        element_1.stop().animate({backgroundColor: "#ffa531", color: "white"}, 300, function(){
            $(this).children().stop().animate({opacity: "1"}, 300);
        });


    };

    if (triggerPoint > element_2.offset.top){

        containers.stop().animate({backgroundColor: "white", color: "#aaa"}, 50);
        element_2.stop().animate({backgroundColor: "#d900ca", color: "white"}, 300, function(){
            $(this).children('img').stop().animate({opacity: "1"}, 300);
        });


    };

    if (triggerPoint > element_3.offset(top)){

        containers.stop().animate({backgroundColor: "white", color: "#aaa"}, 50);
        element_3.stop().animate({backgroundColor: "#d900ca", color: "white"}, 300);


    };

等等,对于 8 到 12 个触发点。

任何帮助将不胜感激!

So, I'm coding up a site that has certain events triggering as you scroll down the page. I want the events to be triggered when the relevant element hits a point just around a quarter of the way down the viewport.

However, this trigger point is obviously different for different sized viewports. I've worked out how to get this trigger point calculated, but I haven't found a way to get the position of a div relative to the top of the viewport/page.

I am trying to use .offset(), which I could combine with getPageScroll() to find the right point, but I can't figure out what on earth to do with the array that it returns. I've also tried popping it in a variable and using that with the syntax I have below (as used on the jquery.com documentation), but it's patently wrong, and returned Undefined in the console.

I am pretty new to both Javascript and jQuery, and to any actual programming in general, so please excuse any stupidity. If I'm doing this all backwards, that's totally a valid answer too! Please just point me in the correct direction if that's the case.

I've coded it up like this so far. The actual effects are only placeholders for now - I'm just trying to get the basic framework working:

// getPageScroll() by quirksmode.com - adapted to only return yScroll
function getPageScroll() {
    var yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
      yScroll = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
    }
    return yScroll
}

// Adapted from getPageSize() by quirksmode.com
function getPageHeight() {
    var windowHeight
    if (self.innerHeight) { // all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }
    return windowHeight
}



var containers = $('div.container');
var element_1 = $('#part_one');
var element_2 = $('#part_two_1');
var element_3 = $('#part_two_2');
var element_4 = $('#part_two_3');
var element_5 = $('#part_two_4');
var element_6 = $('#part_three');
var element_7 = $('#part_four');
var element_8 = $('#part_five');

var docHeight = $(document).height();

$(window).scroll(function() {

    var offset = offset();
    var docHeight = $(document).height();

    var pageBottom = getPageScroll() + getPageHeight(); 

    var quarterPoint = getPageScroll()+((pageBottom-getPageScroll())/4)
    var halfwayPoint = getPageScroll()+((pageBottom-getPageScroll())/2)
    var threeQuarterPoint = pageBottom-((pageBottom-getPageScroll())/4)
    var triggerPoint = quarterPoint-(getPageHeight/10)

    if (triggerPoint < element_1.offset.top){

        containers.stop().animate({backgroundColor: "white", color: "#aaa"}, 50);
        element_1.stop().animate({backgroundColor: "#ffa531", color: "white"}, 300, function(){
            $(this).children().stop().animate({opacity: "1"}, 300);
        });


    };

    if (triggerPoint > element_2.offset.top){

        containers.stop().animate({backgroundColor: "white", color: "#aaa"}, 50);
        element_2.stop().animate({backgroundColor: "#d900ca", color: "white"}, 300, function(){
            $(this).children('img').stop().animate({opacity: "1"}, 300);
        });


    };

    if (triggerPoint > element_3.offset(top)){

        containers.stop().animate({backgroundColor: "white", color: "#aaa"}, 50);
        element_3.stop().animate({backgroundColor: "#d900ca", color: "white"}, 300);


    };

and so on and so forth, for somewhere between 8 and 12 trigger points.

Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半城柳色半声笛 2024-10-27 23:05:59

我在这里看到的几件事:

var offset = offset(); // i don't believe this is a global function.

if (triggerPoint < element_1.offset.top) { // offset needs to be offset(), its a function not a property

Couple things i see here:

var offset = offset(); // i don't believe this is a global function.

if (triggerPoint < element_1.offset.top) { // offset needs to be offset(), its a function not a property
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文