【javascript】javascript实现background-size:cover的功能

    <script type='text/javascript'>
    $(function(){
        var screenWidth = $(window).width();
        var screenHeight = $(window).height();
        //console.log(screenWidth+'//'+screenHeight);
        var screenScale = screenWidth/screenHeight;
        var imgWidth = $('.banner6 img').width();
        var imgHeight = $('.banner6 img').height();
        var scale = imgWidth/imgHeight;
        if(screenScale > scale){
            $('.banner6 img').width(screenWidth);//以宽为标准
            $('.banner6 img').height(screenWidth/scale);
            //console.log('11:'+ $('.banner6 img').width()+':'+$('.banner6 img').height());
        }else{
            $('.banner6 img').width(screenHeight*scale);//以高为标准
            $('.banner6 img').height(screenHeight);
            //console.log('22:'+ $('.banner6 img').width()+':'+$('.banner6 img').height());
        }
    })
    </script>

posted @ 2014-08-26 16:46  Western Journey  阅读(765)  评论(0编辑  收藏  举报