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

时间:2014-08-26 19:02:46   收藏:0   阅读:212
    <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>

bubuko.com,布布扣

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!