Floating left and right positions for a fixed CSS div

My navbar is currently set to float right and 330px left with absolute positioning. Everything is working fine in this setup, but I want it to be fixed. The problem arises when trying to convert it to a fixed position. I've tried various methods but none seem to work. Is there an easy method that I'm missing? Here's my code:

<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

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

$(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>

Answer №1

To position the #nav element on the right side of the screen using position:fixed, you have two options: either add right: 0 or left: 100%, but not both simultaneously. If you want only a small portion of the #nav to be visible by default, you can push the element off-screen by setting a negative margin. For example:

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

Below is a practical demonstration with your code implementing #nav with 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>

Another helpful tip: avoid using JavaScript to alter the background of .boxx during :hover; instead, utilize CSS for this purpose.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the Power of Promise Chaining: Understanding the Distinction between .animate and setTimeout

I am seeking clarification on how promises work in JavaScript. I am struggling to understand the difference between executing a chain composed of jQuery.animate and setTimeout. For example, if I write the code below: var promise = new Promise(function(re ...

The Bootstrap table is not adhering to the specified width when the display is set

I encountered an issue starting yesterday. I am utilizing Bootstrap 4 and tables. Whenever I set a width greater than 13.5vw, the table does not adjust accordingly. You can view the fiddle here Below is the code snippet: HTML <table cl ...

How can I dynamically swap one div with another within the same div using Javascript?

I have a piece of code that I am working with, and I need it to replace every div element whenever a specific div is clicked. function updateContent(target, replacementID) { document.getElementById(target).innerHTML = document.getElementById(replaceme ...

Troubleshooting issue with removing an item from jQuery's html5 localStorage

Exploring the following code snippet: $('#doneItem').click(function(){ $(this).each(function(index){ var item = 'personal' + index; alert(item); localStorage.removeItem('personal' + ...

Javascript - accessing a local file (located in the same directory as the HTML file, not an uploaded file)

I have been attempting to find a solution, but have had no success. Is there a way to read a file using JavaScript or HTML? I have a text file named "sample.txt" with some information in it. This file is located in the same folder as the HTML file that con ...

Lines of text suddenly appearing in HTML email messages on Outlook 2016

We have been utilizing Mailchimp to design simple email templates, exporting the code, and using it with other clients. However, we are encountering random lines under certain tables and between them. I have attempted to address this issue by adding a blac ...

Unable to render Material Icons on Vue/Quasar web app hosted on Azure

I am facing an issue with my webapp built using Vue and Quasar. I added the necessary icon imports to my quasar-user-options.js file. import '@quasar/extras/material-icons/material-icons.css' import "@quasar/extras/material-icons-outlined/ma ...

Tips for keeping buttons anchored at the bottom of the page during scrolling

As a newcomer to Ionic, HTML, and CSS, I am currently exploring how to keep a button fixed while scrolling through the page. Despite specifying a fixed position in the CSS, the button does not remain fixed as intended. The button seems to be having troubl ...

What is the purpose of the `hidden` class for the `<hr>` element?

I've been tasked with updating a webpage that was passed down to me. One thing I'm working on is adding a print.css file, as it didn't have one before. The original developer included a... <hr class="hidden" /> The main css file has ...

Bony Scaffold - halting the motion of specific components

Currently, I am utilizing skeleton to create a fluid layout, which is functioning effectively for the most part. However, I have encountered an issue specifically with the 2 column layout on Magento at this URL: In this setup, the navigation on the left s ...

Implementing JavaScript Code in TypeScript

Every JavaScript code should also be valid in TypeScript, but when attempting to run the following code snippet below, an error is triggered. Could someone convert this JavaScript code into TypeScript? Error: 20:9 - TS2531 Error: Object is possibly 'z ...

Communicating between iframes and parent elements via events

I'm trying to trigger an event in my iframe using the following code: $('body').trigger('my_event'); However, I want to bind this event on my main page that contains the iframe like this: $('iframe').find('body&ap ...

Tips for automatically adjusting the width of dt and dd elements using CSS

My approach to creating a paper form involves using dt and dd elements. The width of dt should align with the word, while dl should occupy the remaining space. Currently, I manually adjust each line, but I am looking for a way to automate this using CSS. ...

Can the script be loaded into a TypeScript file?

I'm currently in the process of integrating this script tag into my Angular 2 project, but I'm searching for a way to incorporate it into the typescript file so that I can access its methods within the .ts file. <script type="text/javascript" ...

What is the process for decoding HTML content that is wrapped within JSON data?

I have a web application built using asp.net that utilizes ajax calls. Below is the code snippet for my web method which successfully responds to the ajax call. ADController adc = new ADController(); DataTable dt = adc.GetGeneral(Convert.ToInt32(A ...

Is there a quicker method for toggling the visibility of an html element?

Is there a more efficient method to show/hide over 20,000 DOM elements? I've noticed that using element.style.display = "none" is very slow. I suspect this is due to too many reflows in the browser. However, simply using element.style.visibility = ...

Creating a functional dropdown form in Ruby On Rails: A Step-by-Step Guide

Having an issue with implementing a dropdown form in the navigation bar of my Rails application. The problem arises randomly - sometimes it works smoothly, while other times I have to refresh the page for it to function properly. The Rails version being u ...

problems with verifying your domain in Google apps

I'm sorry for reaching out here instead of Google's forums, but I haven't had any luck finding answers there. Currently, I have a domain verified on Google Apps by uploading an HTML file, which is being used for email services. I don' ...

Button for Toggling Audio Play and Pause

Is it possible to create an SVG image that changes color slightly when hovered over, and when clicked toggles to another SVG image that can be switched back to the original by clicking again with the same hover effect? Additionally, when clicked, it should ...

jQuery uses variables within the .css() method

Having issues with the code I've written $('.item').each(function(){ var ItemGradient1 = $(this).attr('data-gradient-1'); var ItemGradient2 = $(this).attr('data-gradient-2'); var ItemGradient = 'linear-g ...