我已经和我争斗了很长一段时间,而且运气不好,它总是在各地重新定位!
我基本上希望我的控件将此组件置于页面中间。我还想围绕它的中心轴(Y或X,无关紧要)旋转它(3d),但是我没有运气第一步就是把它带到中心。
<div className="note-container">
<div className="note"
style={Object.assign({}, note.position) }>
<p>{note.text}</p>
<span>
<button onClick={() => onExpand(note.id) }
className="btn btn-warning glyphicon glyphicon-resize-full"/>
<button onClick={() => onEdit(note.id) }
className="btn btn-primary glyphicon glyphicon-pencil"/>
<button onClick={onRemove}
className="btn btn-danger glyphicon glyphicon-trash"/>
</span>
</div>
</div>
我打电话将其重新定位到中心的功能是function onExpand(noteId){...}
以下是.note-container
和.note
div.note-container {
position: fixed;
top: 5%;
left: 90%;
}
div.note {
height: 150px;
width: 150px;
background-color: yellow;
margin: 2px 0;
position: relative;
cursor: -webkit-grab;
-webkit-box-shadow: 5px 5px 15px 0 rgba(0, 0, 0, .2);
box-shadow: 5px 5px 15px 0 rgba(0, 0, 0, .2);
}
div.note:active {
cursor: -webkit-grabbing;
}
div.note p {
font-size: 22px;
padding: 5px;
font-family: "Shadows Into Light", Arial;
}
div.note div.back p {
font-size: 30px;
padding: 5px;
font-family: "Shadows Into Light", Arial;
}
div.note:hover> span {
opacity: 1;
}
div.note> span {
position: absolute;
bottom: 2px;
right: 2px;
opacity: 0;
transition: opacity .25s linear;
}
div.note button {
margin: 2px;
}
div.note> textarea {
height: 75%;
background: rgba(255, 255, 255, .5);
}
这是onExpand函数
onExpand(noteId) {
//This needs a lot of work....
event.preventDefault();
let flippedNote = this.props.notes
.filter(note => note.id === noteId)[0];
flippedNote.position.transition = "1.0s";
flippedNote.position.transformStyle = "preserve-3d";
flippedNote.position.backgroundColor = "#3498db";
flippedNote.position.color = "white";
flippedNote.position.width = "300px";
flippedNote.position.height = "300px";
flippedNote.position.position = "absolute";
flippedNote.position.right = `50% -${flippedNote.position.width / 2}px`;
flippedNote.position.top = `50% -${flippedNote.position.height / 2}px`;
// flippedNote.position.transform = "translate(-50%, -50%) rotateY(180deg)";
this.setState({/*Stuff later... */});
}
当我在页面上渲染笔记时,我会根据此逻辑在页面上为其分配一个随机位置(这是最初传递到div.note元素中的style属性的内容:
position: {
right: randomBetween(0, window.innerWidth - 150) + "px",
top: randomBetween(0, window.innerHeight - 150) + "px",
transform: "rotate(" + randomBetween(-15, 15) + "deg)"
}
以下是页面上的html的样子(注意我也使用transform:translate(...)在页面上拖动粘滞便笺。
答案 0 :(得分:2)
试试这个:
div.note
{
position: relative;
width: 150px;
left: 0;
right: 0;
margin: 2px auto;
}
答案 1 :(得分:1)
要控制位置,您可以在position: absolute;
上设置div.note
或$pull
。
或者,这可以通过操纵边距来完成,但它并不是一个好方法。
您可以通过在浏览器中打开页面并通过Chrome的开发人员工具操作CSS值来手动测试代码。
答案 2 :(得分:1)
这是本周末工作后的最终解决方案:
onExpand(noteId, currentNotePosition) {
event.preventDefault();
let note = this.props.notes
.filter(specificNote => specificNote.id === noteId)[0];
const centerOfWindow = {
left: window.innerWidth / 2,
top: window.innerHeight / 2
};
if (!note.centered) {
note.position.transition = "all 1s";
note.position.transformStyle = "preserve-3d";
note.position.backgroundColor = "#3498db";
note.position.color = "white";
note.position.width = "300px";
note.position.height = "300px";
note.position.zIndex = "100";
note.position.position = "relative";
const offset = {
x: 150,
y: 150
};
const translatedCoordinates = this.getCoordinateTarget(centerOfWindow, offset, currentNotePosition);
note.position.transform = `translate(${translatedCoordinates.x}px, ${translatedCoordinates.y}px) rotateY(180deg)`;
note.originalPosition = {
left: currentNotePosition.left,
top: currentNotePosition.top,
width: currentNotePosition.width,
movement: translatedCoordinates
};
note.centered = true;
} else {
note.position.backgroundColor = "yellow";
note.position.color = "black";
note.position.width = "150px";
note.position.height = "150px";
note.position.transform = "";
note.centered = false;
}
this.props.stickyNoteActions.repositionedNoteSuccess(Object.assign({}, note));
}