javascript - 使用变换比例时如何使图像在 div 中居中

标签 javascript html css

我正在用 JavaScript 编写一些函数,用于使用变换比例进行放大和缩小。这些功能运行良好。问题是缩放后的图像在缩小时向下移动,在放大时向上移动。我想将它保持在 div 的中心。

JSFiddle

HTML:

<div  class="skew"><img id="blah" align="middle" style="margin:0 auto 0 auto;"src="http://3rdbillion.net/wp-content/uploads/2013/11/a24cc9de0c523a5bf7680e18af1952853.jpg"/></div>

<div id="zoom">
    <button type="button" value="zoom in" id="in" onclick="zoomin()" style=" width:80px;height: 25px;">Zoom In</button><br>
    <button type="button" value="zoom out" onclick="zoomout()" id="out"style="height: 25px;" >Zoom Out</button>
</div>

CSS:

.skew {
    display: inline-block;
    height: 120px;
    width: 360px;
    margin-top: 100px;
    overflow: hidden;
}

JS:

function zoomin()
{
    document.getElementById('blah').style.transform = "scale(2,2)";
    document.getElementById('blah').style.webkitTransform = "scale(2,2) ";
    document.getElementById('blah').style.msTransform = "scale(2,2)";
}   


function zoomout()
{
    document.getElementById('blah').style.transform = "scale(0.5,0.5) ";
    document.getElementById('blah').style.webkitTransform = "scale(0.5,0.5)";
    document.getElementById('blah').style.msTransform = "scale(0.5,0.5)";
}

最佳答案

这个怎么样 - FIDDLE .

HTML

<div  class="skew">
    <img id="blah" src="http://3rdbillion.net/wp-content/uploads/2013/11/a24cc9de0c523a5bf7680e18af1952853.jpg"/>
</div>
<br />
<button type="button" value='' id="in">Zoom In</button>
<br />
<button type="button" value="" id="out">Zoom Out</button>

CSS

.skew {
  height: 120px;
  width: 360px;
  margin-top: 100px;
  overflow: hidden;
}
img {
    height: 120px;
    width: 360px;
    margin: 0px auto;
}
button {
    width: 100px;
    height: 25px;
}

JS

$('#in').on('click', function(){
  $('#blah').css("transform", "scale(2, 2)");
                                });

$('#out').on('click', function(){
  $('#blah').css("transform", "scale(0.5, 0.5)");
});

关于javascript - 使用变换比例时如何使图像在 div 中居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22675803/

相关文章:

css - 显示 : css-table vs flex - do i even need the latter?

javascript - 从 'onblur' 事件中排除表单按钮,JavaScript

html - 如何在 <td> 中制作水平滚动条代码块?

php - 每个页面的 Wordpress 不同的标题图像

javascript - 使用内联匿名函数时删除事件监听器

javascript - Safari - 删除 anchor 标记中的文本空间

php - 保护javascript游戏时间

javascript - 有没有更好的方法来编写这个显示/隐藏 JQuery 代码?

javascript - D3-如何在多线图上画点? (遍历数组)

javascript - jQuery隐藏iframe文件上传