如何更改div内的图像边距

时间:2014-10-21 14:01:22

标签: javascript html

这是我的代码

<head>
    <style>
        /* Spritesheet is 2000 x 400 and has 5 frames horizontally */

        .crop {
            width: 400px;
            height: 400px;
            overflow: hidden;
        }
        .crop img {
            width: 2000px;
            height: 400px;
            margin: 0px;
        }
    </style>
</head>

<body>
    <div class="crop">
        <img id="pic" src="spritesheet.png" />
    </div>
</body>

</html>

我想用一个函数将ID pic更改为-400px

2 个答案:

答案 0 :(得分:0)

您可以使用此功能执行此操作:

function MoveImage() {
    // using jQuery
    $('.crop img#pic').css({ 'margin-top': -400 });
    // using javascript
    // document.getElementById('pic').style.marginTop = '-400px';
}

MoveImage();

jsfiddle

答案 1 :(得分:-1)

#pic {margin:20px; }

或者您想要的任何值,您将使用您提供的ID来定位图像本身。 当你通过它的ID定位项目时,你将使用#,当你通过它的目标定位项目时,你会使用。

相关问题