I'm using a counter in the render function so that each span gets it's correct custom property from
state.customProperties
which is an array that updates the custom properties on mouseover of the parent element.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> ); }
In the mousemove function I'm using
document.getElementById
andquerySelectorAll
to get all the section elements and compare the mouse coordinates from the mouse to the section elements coordinates.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 }); }
returnCustomProp
函数返回所述元素的属性的同时避免计数器(在实际代码中,我大约有200个这些元素,因此手动设置数组项编号是低效的)。第二部分感觉像是通过ID来定位实际组件内部的元素,这种方法有点hacky。我认为我应该能够在不遍历DOM的情况下定位到它。引用部分元素可能是一个解决方案,但我认为应该尽量减少引用,正如所述,实际代码由数百个这些部分组成。 JSFIDDLE 目前,该代码并没有做更多的事情,只是更新了
custom="customProperty(0 0)"
属性。您可以通过鼠标悬停在元素检查器上看到这一点。
我能否在不使用document.querySelectorAll
的情况下使此功能正常工作,而无需计算渲染函数中的<section>
元素数量?
this.parentContainer
出现了错误,应该是大写的this.ParentContainer
)。你有什么想法,如何解决在渲染函数内必须使用let counter
的问题? - timo