简体   繁体   English

如何根据位置更改粘性向上箭头按钮的属性?

[英]How to change the properties of a sticky up arrow button based on its position?

I have a sticky up arrow image for my webpage as follows:我的网页有一个粘性向上箭头图像,如下所示:

HTML: HTML:

<a href = "#navbar-scroll"><img src = "images/sticky-btn-light.png" id = "myBtn" alt = "sticky-up-button"></a>

And CSS:和 CSS:

#myBtn {
    visibility: hidden;
    position: fixed;
    bottom: 50px; 
    right: 30px; 
    z-index: 99; 
    cursor: pointer;
    padding: 15px; 
    border-radius: 10px;
    width: 5%;
    opacity: 0.5 ;
}

The button disappears when the user scrolls down and appears when the user scrolls up based on this JS code.按钮在用户向下滚动时消失,在用户向上滚动时出现,基于这段 JS 代码。

window.onscroll = function(e) {
    console.log(this.oldScroll > this.scrollY);
    if((this.scrollY == 0) || (this.oldScroll < this.scrollY)  ){
        document.getElementById('myBtn').style.visibility = 'hidden';
    }
    else if(this.oldScroll > this.scrollY){
        document.getElementById('myBtn').style.visibility = 'visible';
    }
    this.oldScroll = this.scrollY;
}

Now, I want to change the color of the button based on its changing position on the screen.现在,我想根据按钮在屏幕上的位置变化来更改按钮的颜色。 As I scroll the page, the sticky button will be in different sections as below.当我滚动页面时,粘性按钮将位于不同的部分,如下所示。 网页布局1 If the sticky button is in Section A, it should be red.如果粘性按钮在 A 部分,它应该是红色的。 And if it is in Section B, it should be blue.如果它在 B 部分,它应该是蓝色的。 网页布局2 Please note that it's the page that is moving, not the button.请注意,移动的是页面,而不是按钮。 The button is in a fixed position.按钮处于固定位置。

For this, I need the id of the section in which the sticky button is overlapping at any given moment.为此,我需要粘性按钮在任何给定时刻重叠的部分的 ID。 Is there any way to get that information through JavaScript?有没有办法通过 JavaScript 获取这些信息?

PS: I have adjusted the details to make things more clear. PS:我已经调整了细节,使事情更清楚。 It's the page that is moving.是页面在移动。 So, if I use Element.getBoundingClientRect() for #myBtn , will I not get the same top/y values for that element wherever I scroll on the page?因此,如果我将Element.getBoundingClientRect()用于#myBtn ,我在页面上滚动的任何地方都不会获得该元素的相同 top/y 值吗?

You can use element.getBoundingClientRect() to get the x,y of the top left corner and the x,y of the bottom right corner of a element.您可以使用element.getBoundingClientRect()来获取element.getBoundingClientRect()左上角的 x,y 和右下角的 x,y。

var arrow     = document.getElementById('myBtn');
var arrowRect = arrow.getBoundingClientRect();
console.log(arrowRect.top, arrowRect.right, arrowRect.bottom, arrowRect.left);

var section     = document.getElementById('section1');
var sectionRect = section.getBoundingClientRect();
console.log(sectionRect.top, sectionRect.right, sectionRect.bottom, sectionRect.left);

Then you can check collisions with the arrow and the section.然后您可以检查与箭头和截面的碰撞。 In your case the x-axis doesn't matter:在您的情况下,x 轴无关紧要:

// This checks if the arrow is touching the section
( arrowRect.bottom > sectionRect.top && arrowRect.top < sectionRect.bottom  )

// This checks if the arrow isn't touching the section, then inverts it (faster)
!( arrowRect.bottom < sectionRect.top || arrowRect.top > sectionRect.bottom )

Here, I did an example for you to test and implement.在这里,我做了一个例子供大家测试和实现。

this will also help understand getBoundingClientRect even with fixed position即使位置固定,这也将有助于理解getBoundingClientRect

 var arrow= document.getElementById('myBtn'); window.onscroll = function(e) { var arrowBound = arrow.getBoundingClientRect(); var divs = document.querySelectorAll(".container > div"); divs.forEach(function(item){ var divBound = item.getBoundingClientRect(); var color= item.getAttribute("arrowColor"); if ( arrowBound.bottom > divBound.top && arrowBound.top < divBound.bottom ) { arrow.style.borderTopColor = color } }) }
 #myBtn { position: fixed; top: 10px; left:270px; z-index: 99; cursor: pointer; width: 0; height: 0; border-left: 20px solid transparent; border-right: 20px solid transparent; border-top: 20px solid #f00; } .container{ width:300px; height:800px; z-index: 81; } .container > div { width:300px; height:100px; border:1px solid black; }
 <div class="container"> <a id = "myBtn" href = "#navbar-scroll"></a> <div arrowColor="red"> box red </div> <div arrowColor="blue"> box blue </div> <div arrowColor="green"> box green </div> </div>

You can get coords of a html element with getBoundingClientRect() method, call this function and you'll get the Y coords of element (pixels), then you can use an if conditional您可以使用 getBoundingClientRect() 方法获取 html 元素的坐标,调用此函数,您将获取元素(像素)的 Y 坐标,然后您可以使用 if 条件

 function getCoords(elem) { let box = elem.getBoundingClientRect(); let body = document.body; let docEl = document.documentElement; let scrollTop = window.pageYOffset || docEl.scrollTop || body.scrollTop; let clientTop = docEl.clientTop || body.clientTop || 0; return Math.round(box.top + scrollTop - clientTop); } if(getCoords(elem) > 100){ elem.className = 'your_class_name' }

 .your_class_name{ color: red; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM