如何在div内旋转时保持随机输入图像居中

时间:2015-10-06 03:41:45

标签: javascript html css rotation

我正在尝试在显示图像时旋转图像,我无法将图像保持在< div>当我旋转到90度和270度。这是我的代码

.cn {
background: url('images/transparent.png') repeat; 
height: 200px;
position: relative;
vertical-align: middle; 
}

.east_largerwidth {
position: absolute;
-webkit-transform-origin: 75px 125px;
-moz-transform-origin: 75px 125px;
-ms-transform-origin: 75px 125px;
-o-transform-origin: 75px 125px;
transform-origin: 75px 125px;
transform:rotate(270deg);
-ms-transform:rotate(270deg); /* IE 9 */
-webkit-transform:rotate(270deg); /* Safari and Chrome */
}  

.west_largerwidth {
display:block;
margin:auto;
-webkit-transform-origin: 75px 75px;
-moz-transform-origin: 75px 75px;
-ms-transform-origin: 75px 75px;
-o-transform-origin: 75px 75px;
transform-origin: 75px 75px;
transform:rotate(90deg);
-ms-transform:rotate(90deg); /* IE 9 */
-webkit-transform:rotate(90deg); /* Safari and Chrome */
} 

这是旋转显示图像的javascript函数

function rotatefunction() {

    if (document.getElementById("ContentPlaceHolder1_Image1").classList.contains('east_largerwidth')) {

                 img.setAttribute('class', 'north');
                     if (hidderValue > 200) {
                         document.getElementById("ContentPlaceHolder1_Image1").style.width = "auto";
                         document.getElementById("ContentPlaceHolder1_Image1").style.height = "200px";
                     }
} else if ( document.getElementById("ContentPlaceHolder1_Image1").classList.contains('west_largerwidth')) {
             img.setAttribute('class', 'south');
                 if (hidderValue > 200) {
                     document.getElementById("ContentPlaceHolder1_Image1").style.width = "auto";
                     document.getElementById("ContentPlaceHolder1_Image1").style.height = "200px";
                 }
}

html

<div id="divBackground" class="cn" >
  <asp:Image ID="Image1" runat="server" ImageUrl="" style="height: 200px; max-  height: 200px; max-width: 100%;" class='north'/>
</div>
<div class="fileUpload btn btn-warning">
  <input id="inputFile" runat="server" type="file" name="file" onchange="previewFile()" CssClass="upload" />
</div>
<asp:Button UseSubmitBehavior="false" ID="btnRotate" runat="server" Text="Rotate" class="btn btn-warning" OnClientClick="return rotatefunction();" />
旋转0度和180度 0 degree rotate 180 degree

旋转90度和270度 90 degree 270 degree

任何人都可以帮助我如何在旋转到90度和270度时将图像水平保持在中心? 谢谢。

1 个答案:

答案 0 :(得分:0)

center-block {
    display: block;
    margin-left: auto;
    margin-right: auto;
}