0

Even though it seems very easy I am unable to do it.

I have an image inside div and I should move the image up and down not the entire div.

HTML

<div>


<img src="Image/Scope.png" id="indImage" style="height:auto; max-height:80%; width:auto; max-width:90%;" />
</div>

Javascript

  $("#moveUp").on('click',function() {

                alert($('#indImage').offset());


                if(wind_moveup_click != 7){
                $('#indImage').animate({
                marginTop : "-=2px"
                });
   });

But the image is not moving up

Whats the mistake i am doing?

Thanks:)

2
  • Set the height of your container div to a value higher than the image height and check Commented Jul 4, 2013 at 7:21
  • what is "wind_moveup_click "? your code is working with me. i just modify it a little bit. Commented Jul 4, 2013 at 7:24

5 Answers 5

2

Here's the FIDDLE

HTML

<div>
    <img src="Image/Scope.png" id="indImage" style="height:auto; max-height:80%; width:auto; max-width:90%; position:absolute;" />
</div>
<input type="button" id="moveUp" value="Click Me!" style="margin-top:25px;" />

Javascript

$("#moveUp").on('click', function () {

    //alert($('#indImage').offset());
    var wind_moveup_click = 0;

    if (wind_moveup_click != 7) {
        $('#indImage').animate({
            marginTop: "-=2px"
        });
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer has an extra } closing the if statement, which is what makes this work.
2

You had forgoten a } on your if statement. You also need to define the wind_moveup_click variable before using it in the if statement, but maybe you did that, just not in the sample you posted to us.

FIDDLE

and this

if (wind_moveup_click != 7) {
    $('#indImage').animate({
    });
}

Comments

1

Add position:absolute to your img CSS.

#indImage {
    position: absolute;
    height:auto;
    max-height:80%;
    width:auto;
    max-width:90%;
}

JSFIDDLE

Like @Ohgodwhy correctly said:

the position can be anything other than static

That means that you can choose between absolute, relative, fixed and so on.

But another big problem is that you didn't close the opened { in if. So you have a syntax error.

1 Comment

actually, the position can be anything other than static.
0

Try adding the css style position: relative to the <div> tag, e.g.:

<div style="position: relative;">

Comments

0

Edit as,

$('#indImage').animate({
  marginTop : "-2px",
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.