Tôi đã tạo một tập lệnh kích hoạt di chuột trên thùng chứa cha mẹ và sẽ di chuyển phần tử con đó ra khỏi chuột. Hiện tại tôi đã làm cho nó hoạt động nhưng có một số phần của mã dường như mâu thuẫn với mã REACT sẽ trông như thế nào. Đặc biệt là hai phần.
Tôi đang sử dụng một bộ đếm trong chức năng kết xuất để mỗi nhịp có thuộc tính tùy chỉnh chính xác từ
state.customProperties
đó là một mảng cập nhật các thuộc tính tùy chỉnh khi di chuột qua phần tử cha.render() { let counter = 0; const returnCustomProp = function(that) { counter++; let x = 0; if (that.state.customProperties[counter - 1]) { x = that.state.customProperties[counter - 1].x; } let y = 0; if (that.state.customProperties[counter - 1]) { y = that.state.customProperties[counter - 1].y; } return "customProperty(" + x + " " + y + ")"; } return ( <div onMouseMove={this._testFunction} id="ParentContainer"> <section custom={returnCustomProp(this)}> <b>Custom content for part 1</b> <i>Could be way different from all the other elements</i> </section> <section custom={returnCustomProp(this)}> 2 </section> <section custom={returnCustomProp(this)}> <div> All content can differ internally so I'm unable to create a generic element and loop trough that </div> </section> <section custom={returnCustomProp(this)}> 4 </section> <section custom={returnCustomProp(this)}> 5 </section> <section custom={returnCustomProp(this)}> <div> <b> This is just test data, the actualy data has no divs nested inside secions </b> <h1> 6 </h1> </div> </section> <section custom={returnCustomProp(this)}> 7 </section> <section custom={returnCustomProp(this)}> 8 </section> </div> ); }
Trong hàm mousemove tôi đang sử dụng
document.getElementById
vàquerySelectorAll
để lấy tất cả các phần tử phần và so sánh tọa độ chuột từ chuột với tọa độ phần tử phần.var mouseX = e.pageX; var mouseY = e.pageY; var spans = document.getElementById('ParentContainer').querySelectorAll('section'); var theRangeSquared = 10 * 10; var maxOffset = 5; var newCustomProperties = []; for (var i = 0; i < spans.length; i++) { var position = spans[i].getBoundingClientRect(); var widthMod = position.width / 2; var heightMod = position.height / 2; var coordX = position.x + widthMod; var coordY = position.y + heightMod + window.scrollY; // Distance from mouse var dx = coordX - mouseX; var dy = coordY - mouseY; var distanceSquared = (dx * dx + dy * dy); var tx = 0, ty = 0; if (distanceSquared < theRangeSquared && distanceSquared !== 0) { // Calculate shift scale (inverse of distance) var shift = maxOffset * (theRangeSquared - distanceSquared) / theRangeSquared; var distance = Math.sqrt(distanceSquared); tx = shift * dx / distance; ty = shift * dy / distance; } newCustomProperties.push({ x: tx, y: ty }); }
Tôi có cảm giác tôi đang sai về điều này. Tôi không chắc làm thế nào tôi có thể tránh bộ đếm trong khi giữ một returnCustomProp
hàm chung để trả về các thuộc tính cho phần tử đã nói (trong mã trực tiếp tôi có khoảng 200 phần tử này nên việc đặt số mục mảng cho chúng theo cách thủ công là không hiệu quả) .
Phần thứ hai cảm thấy khó khăn khi nhắm mục tiêu một yếu tố bằng ID nằm trong thành phần thực tế. Tôi cảm thấy mình có thể nhắm mục tiêu này mà không cần vượt qua DOM. Tham chiếu các phần tử phần có thể là một giải pháp, nhưng tôi tin rằng các tham chiếu nên được giữ ở mức tối thiểu và như đã nêu, mã thực tế bao gồm hàng trăm phần này.
Mã không làm nhiều atm hơn cập nhật custom="customProperty(0 0)"
tài sản. Bạn có thể thấy điều này xảy ra qua máng thanh tra phần tử trên mouseover.
Tôi có thể làm cho chức năng này hoạt động mà không phải đếm các <section>
yếu tố bên trong chức năng kết xuất và không phải sử dụng document.querySelectorAll
không?
this.parentContainer
btw, nên đượcthis.ParentContainer
viết hoa). Bạn có suy nghĩ gì về cách tôi có thể sử dụnglet counter
chức năng kết xuất bên trong không?