Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

javascript - Make div follows cursor inside parent div - no NPM

In my react component, I have this logic:

My return function is:

return (
      <div>
          <Link
            onMouseEnter={() => this.handleMouseHover(true)}
            onMouseLeave={() => this.handleMouseHover(false)}
            onMouseMove={this.handleCoverTemplateHover}
            style={{position: "relative"}}
            to={link}>
            {this.renderCellContainer(data, selectedSectionIndex, cellIndex)}
            {this.renderHoveredDataOnImages(data,selectedSectionIndex, cellIndex)}
          </Link>
      </div>
    )

The handleMouseHover triggers the hover:

 handleMouseHover = (bool) => {
    this.setState({
      isHovered: bool
    })
  };

The handleCoverTemplateHover sets the state with clientX, Y values:

  handleCoverTemplateHover = (e) =>  {
    this.setState({
      x: e.clientX,
      y: e.clientY
    });
  };

Now, the renderHoveredDataOnImages is as such:

  renderHoveredDataOnImages = (data, selectedSectionIndex, cellIndex) => {

    let id = `catalogue_cell${cellIndex}`;
    let parentDiv = document.getElementById(id);

      if(!parentDiv || !this.state.isHovered){
        return null;
      }

      let getBoundingClientRect = parentDiv.getBoundingClientRect();
      let left = this.state.x - getBoundingClientRect.left;
      let top = this.state.y - getBoundingClientRect.top;

      let dynamicStyling = {
        left: left,
        top: top
      };

      return (
        <div
          style={dynamicStyling}
          className="hovered_catalogue_image_data">
            <div>
              <span>{left}</span>
              <span>{top}</span>
              <span>{data.author}</span>
            </div>
            <div>
              <span>{data.editor}</span>
              <span>{data.dimensions}</span>
              <span>{data.year}</span>
            </div>
        </div>
      )
  };

Here's my use case:

I have a carousel of divs as such, composed of multiple rows. On the first one, the cursor is accurately positioned.

enter image description here

enter image description here

However, when I enter in the second row, the cursor is positioned at the end of the page (it should still follow my cursor) on these second row's divs.

enter image description here

What is happening here?

question from:https://stackoverflow.com/questions/65835126/make-div-follows-cursor-inside-parent-div-no-npm

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...