【发布时间】:2018-08-07 16:35:38
【问题描述】:
我很难让它发挥作用。我从异步事件处理程序转到当前函数。目前最终发生的情况是,如果您选择父 div 中最后一个元素,它最终具有相同的 zIndex,但仍将另一个元素放在顶部。
我基本上是在尝试确保我的 if 语句确保我单击的元素最终具有最高的 zIndex。这是我的整个组件代码,主要看的函数是onStart。我把它弄得一团糟,希望有人能理解我想要完成的事情并帮助我:
import React, {Component} from 'react'
import Draggable from 'react-draggable'
import Router from 'next/router'
import {
BrowserFrame,
DragHandle,
BrowserContent
} from './styles'
class DesktopBrowser extends Component {
constructor(props) {
super(props)
this.state = {
highestIndex: 0
}
}
findHighestZIndex(elem) {
let elems = document.getElementsByClassName(elem);
let highest = 0;
for (let i = 0; i < elems.length; i++) {
let zindex =document.defaultView.getComputedStyle(elems[i],null)
.getPropertyValue("z-index");
if ((zindex > highest) && (zindex != 'auto')) {
this.setState({
highestIndex: zindex
})
}
}
}
onStart(e) {
this.findHighestZIndex("react-draggable")
if(this.state.highestIndex >= 0) {
console.log('greater than zero', this.state.highestIndex, e.currentTarget.style.zIndex)
e.currentTarget.style.zIndex = (+this.state.highestIndex) + (+1)
this.setState({
highestIndex: (+e.currentTarget.style.zIndex) + 1
})
} else if (this.state.highestIndex < e.currentTarget.style.zIndex) {
console.log('lower')
e.currentTarget.style.zIndex = (+this.state.highestIndex) + (+2)
} else {
console.log('catch')
}
// if(this.state.highestIndex >= 0) {
// this.setState({
// highestIndex: (+this.state.highestIndex) + 1
// })
// e.currentTarget.style.zIndex = (+this.state.highestIndex) + 1
// console.log(e.currentTarget.style.zIndex)
// }
}
render() {
return (
<Draggable
bounds="body"
handle=".drag-handle"
onStart={this.onStart.bind(this)}
defaultPosition={
{
x: Math.floor(Math.random() * Math.floor(document.getElementsByClassName('content')[0].offsetWidth - 1000)),
y: Math.floor(Math.random() * Math.floor(document.getElementsByClassName('content')[0].offsetHeight - 500))
}
}>
<BrowserFrame>
<DragHandle className="drag-handle" />
<BrowserContent bg={this.props.content.image}>
<button onClick={(e) => {
e.preventDefault()
Router.push(`/portfolio/${this.props.content.slug}`)
}}>View</button>
</BrowserContent>
</BrowserFrame>
</Draggable>
)
}
}
export default DesktopBrowser
【问题讨论】:
标签: javascript reactjs