将图像移动到不同的位置

时间:2013-05-14 13:21:10

标签: html css image

我一直试图移动小图片(缩略图),但它不会移动。它只是固定在左边。我试过给它一个div id并为它设置边距但它不起作用。

以下是代码:

的JavaScript

function showImage(imgName) {
    document.getElementById('largeImg').src = imgName;
    showLargeImagePanel();
    unselectAll();
}
function showLargeImagePanel() {
    document.getElementById('largeImgPanel').style.visibility = 'visible';
}
function unselectAll() {
    if(document.selection) document.selection.empty();
    if(window.getSelection) window.getSelection().removeAllRanges();
}
function hideMe(obj) {
    obj.style.visibility = 'hidden';
}

CSS

#largeImgPanel {
    text-align: center;
    visibility: hidden;
    position: fixed;
    z-index: 100; 
    top: 0; left: 0; width: 100%; height: 100%;
    background-color: rgba(100,100,100, 0.5);
}

Html正文

<body>
    <img src="images/small1.png" style="cursor:pointer" onclick="showImage('images/large1.jpg');" />
    <img src="images/small2.jpg" style="cursor:pointer" onclick="showImage('images/large2.jpg');" />
    <img src="3_small.jpeg" style="cursor:pointer" onclick="showImage('3_large.jpeg');" />

    <div id="largeImgPanel" onclick="hideMe(this);">
        <img id="largeImg" margin: 0; padding: 0;" />
    </div>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

好吧,如果你正在谈论的是三个img标签,你必须设置他们的风格。类似的东西:

<img src="images/small1.png" class="smallimg" onclick="showImage('images/large1.jpg');" />
<img src="images/small2.jpg" class="smallimg" onclick="showImage('images/large2.jpg');" />
<img src="3_small.jpeg" class="smallimg" onclick="showImage('3_large.jpeg');" />

.smallimg {
    cursor: pointer;
    float: right;
    /* or whatever position/top/left values you need */
}

或者如果你想让它们居中,你可以这样做:

<div style="text-align: center;">
    <img src="images/small1.png" class="smallimg" onclick="showImage('images/large1.jpg');" />
    <img src="images/small2.jpg" class="smallimg" onclick="showImage('images/large2.jpg');" />
    <img src="3_small.jpeg" class="smallimg" onclick="showImage('3_large.jpeg');" />
</div>

答案 1 :(得分:0)

基于我的评论的解决方案。也许你可以通过这个选项:

HTML

<div class="container">
    <img src="http://www.bit-101.com/blog/wp-content/uploads/2009/05/ball.png" alt="" />
    <img src="http://ecx.images-amazon.com/images/I/41P44yRoAPL._SL75_SS50_.jpg" alt="" />
    <img src="http://ecx.images-amazon.com/images/I/41B9Qbj2CBL._SL75_SS50_.jpg" alt="" />
</div>

CSS

.container
{
    height: 50px; /* img height */
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -25px; /* half of the img height */
    margin-left: -75px; /* half of the img width's total */
    overflow: hidden;
}

.container > img
{
    display: inline;
}

http://jsfiddle.net/qFmhf/