Is it possible to only set style.marginLeft one time?

My navigation menu is positioned off-screen to the right, and I am able to hide it by adjusting the style.marginLeft property. However, when I try to reveal it by setting the property again, nothing happens.

function navroll(state) {
  if(state == true) {
    document.getElementById("navbar").style.marginLeft = window.innerWidth + 5 +"px";
    document.getElementById("btn-unhide").style.display = "block";
  } else {
    document.getElementById("navbar").style.marginLeft = window.innerWidth - document.getElementById("navbar").offsetWidth - 5 + "px";
    //document.getElementById("btn-unhide").style.display = "none";
  }
}
function keepscale() {
  if(document.getElementById("btn-unhide").style.display == "block") {
    document.getElementById("navbar").style.marginLeft = window.innerWidth - document.getElementById("navbar").offsetWidth - 5 + "px";
  }
}
#btn-unhide {
  position: fixed;
  display: none;
  left: 90%;
  transition: 3s;
}
#navbar .inner {
  position: absolute;
}
#navbar {
  display: block;
  z-index: 1;
  overflow: hidden;
  background-color: transparent;
  position: fixed;
  left: 60%;
}
#navbar {
  transition: margin-left 3s;
}
.nav-button {
  color: white;
  border-radius: 30px;
  background-color: #f25646;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 16px;
  float: right;
  border-block-start-color: black;
  padding: 10px 16px;
  margin-left: 3px;
  margin-bottom: 3px;
  outline: none;
}
.nav-button:hover {
  background-color: #a7382c;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Revista CNAIC</title>
    <link href="css/navbar.css" rel="stylesheet" type="text/css" />
    <link href="css/index.css" rel="stylesheet" type="text/css" />
  </head>
  <body onresize="keepscale()">
    <div id="navwrapper">
      <div id="btn-unhide">
          <button onclick="navroll(false)"> unhide </button>
      </div>
      <div id="navbar">
        <div class="btn-sign">
        <button class="nav-button">Proiecte si Activitati Extrascolare
        </button>
        <button class="nav-button">Rezultate Academice
        </button>
        <button class="nav-button">Interviul Saptamanii
        </button>
        </div>
        <div id="btn-hide">
          <button onclick="navroll(true)"> hide </button>
        </div>
      </div>
    </div>
    <script src="js/index.js"></script>
    <script src="js/navbar.js"></script>
  </body>
</html>

I've experimented with different methods to reveal it, such as using style.transform = " translate(-" window.innerWidth - myelement.offsetWidth - 5 + "px);" but have had no success. I'm unsure of what else to try.

Answer №1

Is there a reason why you don't simply reset the left margin to 0 to unhide?

function resetMargin(state) {
  if(state == true) {
    document.getElementById("navbar").style.marginLeft = window.innerWidth + 5 +"px";
    document.getElementById("btn-unhide").style.display = "block";
  } else {
    document.getElementById("navbar").style.marginLeft = "0px";
    //document.getElementById("btn-unhide").style.display = "none";
  }
}
function adjustScale() {
  if(document.getElementById("btn-unhide").style.display == "block") {
    document.getElementById("navbar").style.marginLeft = window.innerWidth - document.getElementById("navbar").offsetWidth - 5 + "px";
  }
}
#btn-unhide {
  position: fixed;
  display: none;
  left: 90%;
  transition: 3s;
}
#navbar .inner {
  position: absolute;
}
#navbar {
  display: block;
  z-index: 1;
  overflow: hidden;
  background-color: transparent;
  position: fixed;
  left: 60%;
}
#navbar {
  transition: margin-left 3s;
}
.nav-button {
  color: white;
  border-radius: 30px;
  background-color: #f25646;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 16px;
  float: right;
  border-block-start-color: black;
  padding: 10px 16px;
  margin-left: 3px;
  margin-bottom: 3px;
  outline: none;
}
.nav-button:hover {
  background-color: #a7382c;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Revista CNAIC</title>
    <link href="css/navbar.css" rel="stylesheet" type="text/css" />
    <link href="css/index.css" rel="stylesheet" type="text/css" />
  </head>
  <body onresize="adjustScale()">
    <div id="navwrapper">
      <div id="btn-unhide">
          <button onclick="resetMargin(false)"> unhide </button>
      </div>
      <div id="navbar">
        <div class="btn-sign">
        <button class="nav-button">Proiecte si Activitati Extrascolare
        </button>
        <button class="nav-button">Rezultate Academice
        </button>
        <button class="nav-button">Interviul Saptamanii
        </button>
        </div>
        <div id="btn-hide">
          <button onclick="resetMargin(true)"> hide </button>
        </div>
      </div>
    </div>
    <script src="js/index.js"></script>
    <script src="js/navbar.js"></script>
  </body>
</html>

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

When attempting to execute the "npm run start" command in a ReactJS environment, an error message appeared

'react-scripts' command is not being recognized as a valid internal or external command, able to use program or batch file. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] start: react-scripts start npm ERR! Ex ...

Can anyone suggest a stellar sprite generator for me?

I am in search of a sprite generator to streamline my web development process. With so many options available, I am seeking recommendations for the best tool. Ideally, I want a generator that can produce both a sprite image and corresponding CSS files wi ...

Transmit information from a comprehensive form using AJAX technology

I created a complex form with 30 fields, out of which 3 fields are repeated 10 times. Here's the code: <form id="artikelform" method="POST" name="controleartikelen" action=""> <table id="controle"> <tr><th>Maakartikel</ ...

Having difficulty retrieving an item from a knockout observable array

When fetching data from a web API and pushing it into an observable array, I wanted to make the items in the array observable as well. Unfortunately, I found that I couldn't access the object if I made it observable. function UpdateViewModel() { ...

Troubleshooting CSS Issues in ASP.NET Boilerplate

I am attempting to apply a CSS style to a text input similar to the one found on the CreateUser default page. However, I am encountering an issue where the blue line under the textbox does not appear when I navigate away from and then return to the page. ...

Tips for transferring information from Angular 6 to Node.js

Having recently delved into Angular 6 for the first time, I find myself tasked with sending data to a Node.js server. The code snippet below illustrates my approach within an Angular function: import { Component, OnInit } from '@angular/core'; ...

Expand the dimensions of the bootstrap navigation bar

Is there a way to expand the Bootstrap nav bar to fill the entire screen? Currently, it only occupies half the screen in desktop view, but it is working fine in mobile optimization. <body> <nav class="navbar navbar-expand-lg navbar-light bg-lig ...

Issues with d3.js transition removal functionality not functioning as expected

Having an issue with a d3.js visualization that involves multiple small visualizations and a timeline. When the timeline changes, data points are supposed to be added or removed accordingly. Here is the code snippet responsible for updating: var channels ...

Extract information from various webpages with identical URLs

I am struggling to extract data from a specific webpage (). Even though I can successfully gather information from the initial page, whenever I attempt to navigate to subsequent pages, it continues to display the same dataset. It seems to retrieve the iden ...

What are the best methods for improving the rendering performance of multiple sphereGeometry objects in three.js?

Looking to improve the rendering performance of sphereGeometry in my three.js program, as it is currently a bottleneck. Here is the JavaScript code I am using: var sphereThree = []; for(var idSphere = 0; idSphere < numSphere; idSphere++){ var spher ...

Difficulty filling height with Bootstrap 4 flex-grow

I am in need of creating a layout with a header div at the top (that can be filled with content), a footer div at the bottom, and a middle div with an adaptable height to fill the entire screen. Check out my code below: <div class="container-fluid p-0 ...

Attempting to retrieve a stream buffer from a function

I am currently working on creating a zip file that contains a CSV file and returning it as a buffer from a function. Below is the code snippet I have implemented: const archiver = require('archiver'); const streamBuffers = require("stream-bu ...

Loading a specific number of rows in Datatables upon page loading: How to do it?

Recently, I came across a code snippet that uses AJAX to retrieve data from a specific URL and then displays the information in a table. However, I have a requirement to only load and display the first 10 rows of data during the initial page load process. ...

How to make views in React Native adjust their size dynamically in a scrollview with paging functionality

Has anyone successfully implemented a ScrollView in React Native with paging enabled to swipe through a series of images? I am having trouble making the image views fill each page of the scroll view without hardcoding width and height values for the image ...

The page does not respond to scrolling

I am currently modifying a Wordpress theme and I am looking to increase the size of the main content area. However, when I exceed the screen limits, the content does not scroll. I have identified that the issue is commonly caused by the position: fixed pro ...

Issue with Ajax form submission on one specific page

My form submission using JQuery AJAX is causing my page to redirect to submit.php instead of staying on the current page. I have tried various solutions but cannot pinpoint the issue... The PHP script seems to be working fine, with only one echo statement ...

Is that file or directory non-existent?

While working on developing a Discord bot, I keep encountering an error message stating: "Line 23: no such file or directory, open 'C:\Users\Owner\Desktop\Limited Bot\Items\Valkyrie_Helm.json']" even though the filep ...

Tips for successfully implementing Typeahead with Bloodhound and a JSON response

Looking for guidance on integrating typeahead.js with bloodhound.js to leverage my JSON data structure. My goal is to implement a type-ahead feature utilizing a preloaded JSON object that remains accessible. Here's the breakdown: The Data/All URL res ...

Web links do not adjust properly on mobile pages

Weblinks are causing issues with responsiveness on my website, as they are increasing the mobile page width and resulting in a poor user experience. I am currently using the Asona theme. Please help me resolve this issue - the weblinks are not wrapping t ...

Sending both the minimum and maximum slider values to PHP using POST can be achieved by formatting the data correctly

I am attempting to transmit the minimum and maximum values to PHP using submit through a dual slider bar The static page makes a request to the server-side PHP code <?php include('server/server.php') ?> Below is the JavaScript code ...