div中图像的全宽和高度

时间:2012-11-15 17:34:43

标签: jquery css3 background fullscreen

所以我需要一些帮助。

我做了这个设置,我需要一些图像,总是全屏(缩放到适合)div放在里面。这就是我走了多远,无论我做什么,我都无法让它发挥作用。我真的希望有人可以帮我解决这个问题,它让我疯狂!我试着输入这样的代码

background: url(images/bg.jpg) no-repeat center center fixed; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;

然后图像散开......这是整个代码。

HTML

<div class="wrapper">
    <div class="navigation_top">
    <ul>
        <li>Btn_1</li>
        <li>Btn_2</li>
        <li>Btn_3</li>
        <li>Btn_4</li>
        <li>Btn_5</li>
    </ul>
    </div>

    <div class="navigation_side">
       <ul>        
          <li><a href="#slice1" id="btn_1" class="anchorLink">slice1</a></li>
          <li><a href="#slice2" id="btn_2" class="anchorLink">slice2</a></li>
          <li><a href="#slice3" id="btn_3" class="anchorLink">slice3</a></li>
           <li><a href="#slice4" id="btn_4" class="anchorLink">slice4</a></li>
           <li><a href="#slice5" id="btn_5" class="anchorLink">slice5</a></li>
       </ul>
    </div>

    <div id="slice1"></div>
    <div id="slice2"></div>
    <div id="slice3"></div>
    <div id="slice4"></div>
    <div id="slice5"></div>
</div>

CSS

html, body {height:100%; color:#FFF;}

ul, ol, li {margin:0px!important; padding:0px!important;}

.wrapper {width:100%; height:100%;}
.navigation_top {width:100%; height:50px; line-height:50px; background-color:#000; opacity:.5; position:fixed;}
.navigation_top ul {list-style:none;}
.navigation_top ul li {float:left; width:100px; text-align:center;}
.navigation_top ul li a {display:block; color:#FFF; text-decoration:none;}



.navigation_side {width:200px; height:auto; position:fixed; background-color:#000; opacity:.5; margin-top:10%;}
.navigation_side ul li a {color:#FFF; text-decoration:none;}


#slice1 {width:100%; height:100%; background:url(http://img405.imageshack.us/img405/6018/image1uii.jpg);}
#slice2 {width:100%; height:100%; background-color:#999;}
#slice3 {width:100%; height:100%; background-color:#888;}
#slice4 {width:100%; height:100%; background-color:#777;}
#slice5 {width:100%; height:100%; background-color:#666;}

JQUERY

jQuery(document).ready(function($) {

    $(".anchorLink").click(function(event){        
        event.preventDefault();
        $('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
    });

    $('.navigation_top').hide();

    $(window).scroll(function() {
        // the 10 below is the number of pixels down to show the menu
        if ($(this).scrollTop() > 10) $('.navigation_top').show();
        else $('.navigation_top').hide();
    });
});

这也是我进步的一个小提琴:http://jsfiddle.net/iBertel/qLTQ9/

1 个答案:

答案 0 :(得分:0)

您确定这是适合屏幕的最佳方式吗?我只是因为关于图像分辨率的中心而对它有所不同。

我这样做的方法是先检查屏幕的宽度/高度。然后我将它与图像的宽度/高度进行比较。然后,如果图像比屏幕大,我会压缩它,这样我就可以将它放到屏幕上了。

如果图像小于屏幕,那么我将图像居中在div中并将div背景的颜色设置为好的(例如,如果这与您的网站颜色匹配,请确保它是黑色div)

请记住,您必须检查图像相对于屏幕宽度/高度的宽度/高度。例如,如果图像对于屏幕来说太宽,但不是很高,则应该按照宽度/高度比例缩小图像,因此它将在div内显示为水平矩形。这样您的图像就不会失真。

相关问题