0

Hi I'm trying to change background gradually on button click on my web page this is a javascript and html code snippets what am I doing wrong?

var content=document.getElementById('content').style;
var content1 = document.getElementById('content1').style;
var start1;
var start2;
var inti =null; 
function changeContent(){       
    start1 =1;
    start2 = 1001;  
    inti=setInterval('swapContent()',20);       
}

function swapContent(){  
    start1 -=5;
    start2 -=5;
    content.top = start1 + 'px';
    content1.top = start2 + 'px';                        
} 

 <body>

    <div id="navigation" >
    <input type="button" onclick="changeContent()" />
    </div>
    <div id="content">
    </div>
    <div id="content1">
    </div>        
</body>
3
  • 1
    In what way does the code not work? (Also, don't call setInterval with a string, and ideally don't call Javascript with HTML attributes...) Commented Nov 23, 2013 at 12:35
  • positions of the content doesn't change Commented Nov 23, 2013 at 12:39
  • Why not use CSS transitions, that's what they're for. Commented Nov 23, 2013 at 12:40

1 Answer 1

1

If you want style top to work, you should set css position property. See fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

I have set position attribute too absolute

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.