【问题标题】:are querySelectorAll and getBoundingClientRect compatible?querySelectorAll 和 getBoundingClientRect 兼容吗?
【发布时间】:2021-07-20 00:30:33
【问题描述】:

我是一个非常缺乏经验的开发人员。我正在努力寻找解决方案。我有两个具有相同类的按钮,以及一个名为 cursorFollower 的 div,它实际上遵循实际的 pageX 和 pageY 定位。上周,感谢一些人的帮助,我设法根据悬停的按钮更改了 cursorFollower 的形状。问题是我无法让它与同一类的所有按钮一起使用。有人建议我将它从 querySelector 更改为 querySelectorAll 以选择具有相同类的所有元素,但是如何知道一次悬停的是哪个元素?同样出于某种原因,在使用 codepen 时,它一直说 getBoundingClientRect 现在应该在一个函数中,它将按钮的大小和位置作为单独的变量获取。我已将所有内容都包含在一个函数中,但什么都没有发生。有人可以告诉我我做错了什么并建议我该怎么做吗?

let cursor = document.querySelector('.cursorFollower');
let button = document.querySelector('.menutext');

let buttonWidth = button.getBoundingClientRect().width;
let buttonHeight = button.getBoundingClientRect().height;
let buttonX = button.getBoundingClientRect().left;
let buttonY = button.getBoundingClientRect().top;

var onContainer = false;

const handleCursor = (e) => {
    if (onContainer) {
      cursor.setAttribute(
        "style",
        `left: ${buttonX}px;top: ${buttonY}px;width: ${buttonWidth}px;height: ${buttonHeight}px; transform: rotate(0deg); border: 1px solid #84c4b5;`
      );
      button.setAttribute(
        "style",
        `color: #84C4B5;`
      );
    } else {
      cursor.setAttribute(
        "style",
        `left: width: 20px; height: 20px; transform: rotate(45deg); border: solid 1px #ffffff;`
      );
      cursor.style.left = e.pageX - 10 + 'px';
      cursor.style.top = e.pageY - 10 + 'px';  
      button.setAttribute(
        "style",
        `color: #ffffff;`
      );
    }
};


document.addEventListener("mousemove", handleCursor);

button.onmouseover = () => {
  onContainer = true;
};

button.onmouseleave = () => {
  onContainer = false;
};
*{
  margin: 0;
  padding: 0;
}

.container {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width:100%;
  height: 100vh;
  background-color: #0A193E;
  color: white;
  font-family: sans-serif;
  font-size: 20px;
}

.container2 {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width:100%;
  height: 100vh;
  background-color: green;
  color: white;
  font-family: sans-serif;
  font-size: 20px;
}

.menutext {
  padding: 10px 20px;
  cursor: none;
  transition: all 0.2s ease;
}

.cursorFollower {
  width: 20px;
  height: 20px;
  position: absolute;
  border: 2px solid white;
  transition: all 0.2s ease-out;
  pointer-events: none;
  transform: rotate(45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container">
  <p class="menutext">button1</p>
  <p class="menutext">button2</p>
</div>

<div class="container2">
  <p class="menutext">button3</p>
</div>

<div class="cursorFollower"></div>

【问题讨论】:

    标签: javascript html jquery css


    【解决方案1】:

    最简单的做法是完全取消按钮 mouseover/out 处理程序,并在 handleCursor 函数中处理这一切。如果事件 target 具有类 menutext 应用您的效果,但请注意您需要将光标 y 位置偏移 window.scrollY 值:

    let cursor = document.querySelector('.cursorFollower');
    
    const handleCursor = (e) => {
        var button = e.target;
        if (button.classList.contains("menutext")) {
          const boundingRect = button.getBoundingClientRect();
          let buttonWidth = boundingRect.width;
          let buttonHeight = boundingRect.height;
          let buttonX = boundingRect.left;
          let buttonY = window.scrollY + boundingRect.top;
          cursor.setAttribute(
            "style",
            `left: ${buttonX}px;top: ${buttonY}px;width: ${buttonWidth}px;height: ${buttonHeight}px; transform: rotate(0deg); border: 1px solid #84c4b5;`
          );
          button.setAttribute(
            "style",
            `color: #84C4B5;`
          );
        } else {
          cursor.setAttribute(
            "style",
            `left: width: 20px; height: 20px; transform: rotate(45deg); border: solid 1px #ffffff;`
          );
          cursor.style.left = e.pageX - 10 + 'px';
          cursor.style.top = e.pageY - 10 + 'px';  
          button.setAttribute(
            "style",
            `color: #ffffff;`
          );
        }
    };
    
    
    document.addEventListener("mousemove", handleCursor);
    *{
      margin: 0;
      padding: 0;
    }
    
    .container {
      position: relative;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width:100%;
      height: 100vh;
      background-color: #0A193E;
      color: white;
      font-family: sans-serif;
      font-size: 20px;
    }
    
    .container2 {
      position: relative;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width:100%;
      height: 100vh;
      background-color: green;
      color: white;
      font-family: sans-serif;
      font-size: 20px;
    }
    
    .menutext {
      padding: 10px 20px;
      cursor: none;
      transition: all 0.2s ease;
    }
    
    .cursorFollower {
      width: 20px;
      height: 20px;
      position: absolute;
      border: 2px solid white;
      transition: all 0.2s ease-out;
      pointer-events: none;
      transform: rotate(45deg);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class="container">
      <p class="menutext">button1</p>
      <p class="menutext">button2</p>
    </div>
    
    <div class="container2">
      <p class="menutext">button3</p>
    </div>
    
    <div class="cursorFollower"></div>

    【讨论】:

    • 感谢您帮助我更好地理解@Jamiec。现在我会花时间消化这些变化并完全理解这里发生了什么:)!
    • @Eu2019 我基本上已经将获取 1 个按钮的边界矩形详细信息的代码移到了函数中,因此它适用于 any 按钮。请注意,我还进行了更新,因此我没有重复 button.getBoundingClientRect() 部分(请参阅:不要重复自己!)
    • 感谢您的解释。我现在明白了,快速提问,我也更改了按钮文本颜色,但现在在实施时影响了所有可点击元素。我已从按钮更改为此按钮,但现在无法正常工作。我仍在试图理解,因为 if 语句说“如果目标元素,在移动鼠标时有一个类“menutext”然后做正确的事情?在那个条件中包含了 button.setAttribute 样式颜色 #...
    • 所以现在其他目标元素的颜色也发生了变化,我只希望“menutext”元素在被定位并且仅在鼠标悬停时改变颜色,所以我应该在其中包含鼠标悬停事件事件?@Jamiec。
    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 2014-08-04
    • 1970-01-01
    相关资源
    最近更新 更多