简体   繁体   English

CSS固定div,左右浮动

[英]CSS fixed div with float right and left positioning

So I have navbar with float right and 330px left. 所以我有导航栏,向右浮动,向左浮动330px。 Everything is working fine with absolute position but I want fixed and here comes the problem. 一切都在绝对位置上运行良好,但我想修复,问题就来了。 How I'm suposed to do it ? 我该怎么做呢? I also tried various of methods but they don't seem to work I really hope that there is some really easy method but I can't figure it out. 我也尝试了各种方法,但是它们似乎不起作用,我真的希望有一些非常简单的方法,但我无法弄清楚。 Here is my code HTML 这是我的代码HTML

<div id="nav" >
    <ul>
        <li><img class="toggle-slide-button" src="assets/images/menu/menutoggle.png" alt=""></li>
        <li><img class="Menu_icon"src="assets/images/menu/Home_icon.png" alt="">
        <div class="menu_text">Home</div>
        <img  class="boxx" src="assets/images/menu/box.png" alt="">

        </li>

    </ul>

</div>

CSS CSS

body{
    overflow-x: hidden;
    background: url(../images/Home/Screen_1/home_final_background.png) 100% 0 fixed;
}
ul{
    list-style-type:none
}
.Menu_icon{
    position: absolute;
    z-index: -1;
}
#nav{ float: right;  z-index: 152; right: -330px; 

    margin: 0;
    padding: 0;
    text-align: initial !important;
    position: fixed;
}
.menu_text{
    color:white;
    font-size:24px;
    height: 132px;
    width: 390px;
    position: absolute;
    margin-left: 80px;
    margin-top: 60px;
    z-index: -2;
}

JS JS

        $(document).ready(function(){
    var state = true;

    $(".toggle-slide-button").click(function () {
        if (!state) {
            $('#nav').stop().animate({left:330}, 1000);
              state = true;
            }
        else {
              $('#nav').stop().animate({left: 20}, 1000);
              state = false;
            }
    });
    });



 $(document).ready(function(){
    $(".boxx").hover(function() {
        $(this).attr("src","assets/images/menu/box_hover.png");
            }, function() {
        $(this).attr("src","assets/images/menu/box.png");
    });
});

EDIT: here is what I want to make 编辑:这是我想做的

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <style> img{ user-drag: none; -moz-user-select: none; -webkit-user-drag: none; } body{ overflow-x: hidden; background: url(http://i.imgur.com/z1y0hYN.png) 100% 0 fixed; } ul{ list-style-type:none } .Menu_icon{ position: absolute; z-index: -1; } #nav{ float: right; left:330px; margin: 0; padding: 0; position: relative; } .menu_text{ color:white; font-size:24px; height: 132px; width: 390px; position: absolute; margin-left: 80px; margin-top: 60px; z-index: -2; } </style> <script> $(document).ready(function(){ var state = true; $(".toggle-slide-button").click(function () { if (!state) { $('#nav').stop().animate({left:330}, 1000); state = true; } else { $('#nav').stop().animate({left: 20}, 1000); state = false; } }); }); </script> <script type='text/javascript'> $(document).ready(function(){ $(".boxx").hover(function() { $(this).attr("src","http://i.imgur.com/MlmFvp3.png"); }, function() { $(this).attr("src","http://i.imgur.com/FCKVSFe.png"); }); }); </script> <div id="nav"> <ul> <li><img class="toggle-slide-button" src="http://i.imgur.com/WsgovYE.png" alt=""></li> <li><img class="Menu_icon"src="http://i.imgur.com/rYo7Utj.png" alt=""> <div class="menu_text">Home</div> <img class="boxx" src="http://i.imgur.com/FCKVSFe.png" alt=""> </li> </ul> </div> 

When using position:fixed you can position the #nav element to the right side of the screen by either adding: right: 0 or left: 100% (not both). 当使用position:fixed您可以通过添加: right: 0left: 100% (不是全部)将#nav元素定位在屏幕的right: 0 Since you only want a small part of the #nav to be visible by default, you can push the element 'out of the screen' by setting a negative margin, for example: 由于默认情况下您只希望#nav一小部分可见,因此您可以通过设置负边距将元素“推出屏幕”,例如:

#nav {
    position: fixed;
    left: 100%;
    width: 400px;
    margin-right: -300px; /* (400px-300) keeps 100px of the element in sight */
}

Here's a working example of your code with #nav set to position:fixed : 这是#nav设置为position:fixed代码的工作示例:

 $(document).ready(function() { var state; $(".toggle-slide-button").click(function() { if (!state) { $('#nav').stop().animate({ 'margin-left': '-330px' }, 1000); state = true; } else { $('#nav').stop().animate({ 'margin-left': '-65px' }, 1000); state = false; } }); }); $(document).ready(function() { $(".boxx").hover(function() { $(this).attr("src", "http://i.imgur.com/MlmFvp3.png"); }, function() { $(this).attr("src", "http://i.imgur.com/FCKVSFe.png"); }); }); 
 .just-a-dummie-element { height: 2600px; } body { overflow-x: hidden; background: url(http://i.imgur.com/z1y0hYN.png) 100% 0 fixed; } ul { list-style-type: none; margin: 0; padding: 0; } .toggle-slide-button { display: block; cursor: pointer; } .Menu_icon { position: absolute; z-index: -1; display: block; cursor: pointer; } #nav { position: fixed; top: 40px; left: 100%; margin: 0 0 0 -65px; padding: 0; } .menu_text { color: white; font-size: 24px; height: 132px; width: 390px; position: absolute; margin-left: 80px; margin-top: 60px; z-index: -2; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="just-a-dummie-element">..just to make the page loger!</div> <div id="nav"> <ul> <li> <img class="toggle-slide-button" src="http://i.imgur.com/WsgovYE.png" alt="" /> </li> <li> <img class="Menu_icon" src="http://i.imgur.com/rYo7Utj.png" alt="" /> <div class="menu_text">Home</div> <img class="boxx" src="http://i.imgur.com/FCKVSFe.png" alt="" /> </li> </ul> </div> 

Also, just a simple tip: don't use javascript to change the background of the .boxx on :hover , just use css. 另外,一个简单的提示:不要使用javascript来更改.boxx的背景:hover ,而应使用css。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM