【问题标题】:Positioning this element to the center of the page - CSS将此元素定位到页面的中心 - CSS
【发布时间】:2016-09-07 06:32:28
【问题描述】:

我已经为此奋斗了很长一段时间,但运气并不好,它总是会到处重新定位!

我基本上希望我的控件将这个组件放在页面中间。我也想围绕它的中心轴(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 元素中 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(...) 在页面上拖动便笺。

【问题讨论】:

    标签: javascript html css reactjs


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 运气不好,我觉得我的其他风格正在压倒所有这些
    【解决方案2】:

    要控制位置,您可以在div.note 上设置position: relative;position: absolute;

    或者,这可以通过操纵边距来完成,但这并不是一个好方法。

    您可以通过在浏览器中打开页面并通过 Chrome 的开发人员工具操作 CSS 值来手动测试您的代码。

    【讨论】:

    • 我都试过了,但我觉得我已经碰壁了,因为我一直在修改翻译和其他样式。根据上图你觉得怎么样?
    【解决方案3】:

    这是本周末工作后的最终解决方案:

    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));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 2014-11-24
      • 2012-02-04
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      相关资源
      最近更新 更多