javascript - 将此元素定位到页面的中心 - CSS

标签 javascript html css reactjs

我已经和这个问题斗争了很长一段时间,但我运气不佳,它总是在各处重新定位!

我基本上希望我的控件将此组件置于页面中间。我还想围绕它的中心轴(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 的 CSS

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 元素中的样式属性的内容:

position: {
                    right: randomBetween(0, window.innerWidth - 150) + "px",
                    top: randomBetween(0, window.innerHeight - 150) + "px",
                    transform: "rotate(" + randomBetween(-15, 15) + "deg)"
                }

这是页面上的 html 的样子(请注意,我还使用 transform: translate(...) 在页面上拖动便签。

enter image description here

最佳答案

试试这个:

div.note 
 {
    position: relative;
    width: 150px;
    left: 0;
    right: 0;
    margin: 2px auto;
 }

关于javascript - 将此元素定位到页面的中心 - CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39302628/

相关文章:

javascript - 框架 + Internet Explorer 怪癖

javascript - 当用户单击按钮时,如何使我的页面向下滚动?

css - 使用 css 的序列号

javascript - jQuery Steps 新步骤被添加到新行

javascript - 使用 javascript 在 html 中使用单选按钮控件很困难

javascript - 当使用 [Enter] 键并存在多个表单时提交预期 HTML 表单的通用解决方案?

jquery - 如何通过Google脚本在Drive中的文件夹内创建文件夹?

html - 带有单选按钮的表格不起作用

javascript - 以编程方式填充组合框 dojo (1.8) 的最佳方法是什么?

javascript - 相同的添加操作但 Javascript 中的结果不同