【问题标题】:Scale div and move child elements relatively缩放 div 并相对移动子元素
【发布时间】:2015-07-16 18:09:41
【问题描述】:

我需要放大或缩小背景图像,并保持其中的子 div 相对于比例

当我向上或向下缩放父 div 时,绿色方块应该保持它们的位置(与它们最初与图像对齐)

绿色 div 不应更改其大小。

如果您需要更多信息,请告诉我

JS 小提琴在这里http://jsfiddle.net/87cmgp8a/

HTML

<div id="container">

    <div id="imageContainer">

        <div id="square1" class="square"></div>
        <div id="square2" class="square"></div>
        <div id="square3" class="square"></div>

    </div>
</div>
<br><br>
<button id="scaleup">Scale Up </button>
<button id="scaledown">Scale Down </button>

CSS

#container {
    overflow: scroll;    
    height:350px;
    width:350px;
}

#imageContainer {
    background-image: url("http://www.portangelesschools.org/students/images/clip_image015.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    position: relative;
    height:400px;
    width: 400px;
}

.square {
    border: 1px solid;
    background-color: green;
    width: 35px;
    height: 35px;
    position: absolute;
}

#square1{ top: 8px; left: 87px; }
#square2{ top: 88px; left: 87px; }
#square3{ top: 165px; left: 87px; }

JS

var $imageContainer = $("#imageContainer");
var scaleFactor = 50;

$("button").click(function(){

    var id = $(this).attr("id");
    if( id === "scaleup" ) {
        $imageContainer.css({
            height: $imageContainer.height() + scaleFactor, 
            width: $imageContainer.width() + scaleFactor
        });
    }
    else {
        $imageContainer.css({
            height: $imageContainer.height() - scaleFactor, 
            width: $imageContainer.width() - scaleFactor
        });
    }

});

【问题讨论】:

    标签: jquery html css css-position scaling


    【解决方案1】:

    尝试设置 CSS 变换而不是高度和宽度:

    transform: scale(x, y);
    

    【讨论】:

    • 感谢您的建议-我尝试在小提琴中进行转换-但它也会缩放所有子元素-我想保持相同大小的绿色 div 并仅缩放父 div-更新了问题
    • 尝试将子级的比例设置得更小,同时将父级的比例设置得更大。
    • 不,它没有 - 这会导致子元素异常模糊
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2015-01-19
    • 2017-02-20
    相关资源
    最近更新 更多