Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to change background of html page in mvc dynamically.Any idea??
I have trie like this

JavaScript
 <script type="text/javascript">
        var background = ['@Url.Content("~/Content/images/ab.jpg")',
                '@Url.Content("~/Content/images/gh.jpg")',
                '@Url.Content("~/Content/images/hj.jpg")',
                '@Url.Content("~/Content/images/i8.jpg")'];
                

        $(document).ready(function () {
            PickRandomBackground();
        });

        function PickRandomBackground() {
            var index = Math.floor(Math.random() * 5);
            $('html').css('background-image','url('+ background[index]+')')
        }
</script>


and my .css is
CSS
html
{
    background-image:url(images/ab.jpg);
    background-repeat:repeat;
}


But it shows only ab.jpg
Posted
Comments
[no name] 9-Mar-15 6:20am    
Try background-repeat: no-repeat;

 
Share this answer
 
<script type="text/javascript">

    $(window).load(function () {
    
        var i = 0;
    
        var images = ['@Url.Content("~/Content/images/i8.jpg")',
                '@Url.Content("~/Content/images/gh.jpg")',
                '@Url.Content("~/Content/images/hj.jpg")',
                '@Url.Content("~/Content/images/11.jpg")'];
       
        var image = $('#slideit');
   
        //Initial Background image setup
     
        image.css('background-image', '~/Content/images/i8.jpg');

        //Change image at regular intervals

        setInterval(function () {
     
            image.fadeOut(1000, function () {
            
                image.css('background-image', 'url(' + images[i++] + ')');
            
                image.fadeIn(1000);
             
            });

            if (i == images.length)
    
            i = 0;
           
        }, 8000);
   
    });
   
</script></script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900