Move a div horizontally by 100% using translateX, then bring it back into view by a specific number of pixels

I am currently working on creating a custom push navigation system and I am using translateX to shift the content over to reveal the navigation menu.

However, I am facing an issue where I don't want the content to be completely pushed off screen, but instead have a small portion of it visible on the right side.

The challenge is that I can't set a fixed pixel value for this adjustment as it needs to adapt responsively. One idea I had was to translateX by 100% and then somehow bring it back by about 50px, but I'm struggling to implement this solution.

$(document).on('click','.wrapper span', function() {
  $('.wrapper').addClass('active');
});
html, body {
  height:100%;
}
.wrapper {
  background:red;
  height:100%;
}
.wrapper span {
  width:50px;
  height:50px;
  background:yellow;
}
.wrapper.active {
  transform:translateX(100%);
}
.navigation {
  position:absolute;
  background:#666666;
  color:#ffffff;
  top:0px;
  left:0px;
  bottom:0;
  z-index:-1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  i am a wrapper
  <span>click me</span>
</div>

<div class="navigation">
  i am a nav
</div>

DEMO

Answer №1

Have you considered utilizing the calc function for this task? (For browser compatibility information, see here)

Here is the relevant code snippet:

.wrapper.active {
  transform:translateX( calc(100% - 50px) );
}

Check out the complete example below:

$(document).on('click','.wrapper span', function() {
  $('.wrapper').addClass('active');
});
html, body {
  height:100%;
}
.wrapper {
  background:red;
  height:100%;
}
.wrapper span {
  width:50px;
  height:50px;
  background:yellow;
}
.wrapper.active {
  transform:translateX( calc(100% - 50px) );
}
.navigation {
  position:absolute;
  background:#666666;
  color:#ffffff;
  top:0px;
  left:0px;
  bottom:0;
  z-index:-1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  i am a wrapper
  <span>click me</span>
</div>

<div class="navigation">
  i am a nav
</div>

You can also concatenate transforms together in this manner:

.wrapper.active {
  transform:translateX(100%) translateX(-50px);
}

Complete example using this method:

$(document).on('click','.wrapper span', function() {
  $('.wrapper').addClass('active');
});
html, body {
  height:100%;
}
.wrapper {
  background:red;
  height:100%;
}
.wrapper span {
  width:50px;
  height:50px;
  background:yellow;
}
.wrapper.active {
  transform:translateX(100%) translateX(-50px);
}
.navigation {
  position:absolute;
  background:#666666;
  color:#ffffff;
  top:0px;
  left:0px;
  bottom:0;
  z-index:-1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  i am a wrapper
  <span>click me</span>
</div>

<div class="navigation">
  i am a nav
</div>

Answer №2

Utilizing negative margin can be a clever technique

.wrapper.active{
  position: relative;
  left: 100%;
  margin-left: -50px;
}

EDIT

Achieving the same effect without using relative positioning

.wrapper.active{
  margin-left: calc(100% - 50px);
  margin-right: calc(-100% + 50px);
}

$(document).on('click', '.wrapper span', function() {
  $('.wrapper').addClass('active');
});
html,
body {
  height: 100%;
  margin: 0;
}
.wrapper {
  background: red;
  height: 100%;
}
.wrapper span {
  width: 50px;
  height: 50px;
  background: yellow;
}
.wrapper.active {
  margin-left: calc(100% - 50px);
  margin-right: calc(-100% + 50px);
}
.navigation {
  position: absolute;
  background: #666666;
  color: #ffffff;
  top: 0px;
  left: 0px;
  bottom: 0;
  z-index: -1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="wrapper">
  i am a wrapper
  <span>click me</span>
</div>

<div class="navigation">
  i am a nav
</div>

Answer №3

If you're looking to achieve this with jQuery, you can utilize the hide and show functions. Start by initializing the menu and content, but at the beginning (using document.ready()), hide the menu. Then, show it when clicked using onclick as shown below:

<div class="wrapper">
  i am a wrapper
  <span>click me</span>
</div>

<div class="navigation">
  i am a nav
</div>

The wrapper and navigation elements should be positioned side by side, starting with the navigation element.

$(document).ready(function() {
    $('.navigation').hide();
    $('.wrapper').click(function() {
        $('.navigation').show(100);
    });    
});

Using this method will ensure that the wrapper and navigation stay together and don't get separated.

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

What could be causing my CSS formatting from a linked style sheet to not be applied properly?

I've been trying to incorporate a YouTube video into my webpage using a CSS stylesheet, but I can't seem to get the video to display properly .youtube-video iframe, .youtube-video img, .youtube-video-2 iframe, .youtube-video-2 img { positi ...

Text parsing with jQuery

Hello fellow developers. I am interested in creating a text parser using jQuery. My goal is to develop a function that can take a string as input and convert it accordingly. Imagine we have a div with the following HTML structure: <div class="item"> ...

Custom field data in WordPress

I'm having difficulty formatting the prices of certain products using a custom field (unit_price). The value is 3.2, but the code only displays 3 because it doesn't recognize commas or dots. Is there a way to display the full value? any assistanc ...

Incorporating an HTML Canvas element within a CSS grid design

I've been attempting to integrate an html canvas that I created into one of the css grid classes specified in my code. The code runs when the "Calculate" button is clicked. Upon clicking the button, the <div> section designated for the canvas r ...

Show the outcome of a mysql_query in PHP directly within MYSQL

I've been trying to figure out how to display my MySQL results in PHP using tables. Despite doing a lot of research on this topic, I haven't been able to find a solution yet. Here's the code that I have: <html> <head> </head& ...

Fixed Positioning Div to Stay at the Top while Scrolling

Currently, I have successfully implemented the functionality to stick the div to the top once it scrolls down by 320px. However, I am curious if there is an alternative approach to achieving this effect. Below is the code snippet I am using: jQuery(functi ...

Are you transitioning from traditional scroll pagination to using ajax?

Is it possible to replace scroll pagination with ajax? I'm looking for an alternative to the large scroll pagination query and wondering if ajax could be used instead. Here is the current code snippet: feeds.scrollFeedPagination({ 'contentPage ...

Enhance the appearance of your webpage by utilizing the Bootstrap round-circle class along with

I am facing an issue with my Bootstrap carousel that contains multiple items. Here is my HTML code: <div class="carousel w-100 " data-ride="carousel" id="recipeCarousel"> <div class="carousel-inner w-100" role="listbox"> <div c ...

Example using three.js showing issues with external resources failing to load on jsfiddle.net

Currently, I am endeavoring to make progress with this sample project: github.com/josdirksen/learning-threejs/blob/master/chapter-09/07-first-person-camera.html I have made attempts at replicating the code on my personal pages.github.io account and also m ...

Guide to opening a link in a new tab within the same webpage

I have observed numerous web applications that feature a single web window with multiple links. When clicking on the links, the corresponding form opens in a new tab within the same web page of the application. I'm curious about how to achieve this fu ...

Tips for populating two select tags with jQuery AJAX using a JSON document

Looking for help with my HTML code that includes two select tags. I want to use AJAX to populate the 'collegeselect' based on the selection in the 'departmentselect'. HTML CODE titled 'collegedepartment.html' <html> ...

Upon installation, the extension that replaces the new tab fails to detect the index.html file

edit: Check out the Chrome Extension here Edit 2: It seems that the recent update containing the index.html file was not published due to Google putting it under revision. Apologies for forgetting to include the index.html file in the upload zip, as I ...

Ensure data is validated before sending an ajax post request

Currently, I am developing an ASP.Net MVC 2 application where I need to implement validation for a specific pop-up using jQuery. This is necessary because the data from the pop-up will be sent through an AJAX call as shown in the code snippet below. Note: ...

Utilizing the Jquery Number Spinner for Updating Quantities in the Database

I’m just starting to learn JQuery/JavaScript/Ajax, as well as Symfony2 & PHP. Currently, I am working on a basic Shopping Cart project where I want users to be able to adjust the quantity of a product they have added to their cart using the JQuery Spinn ...

Fade out the jQuery message after a brief 2-second pause

In my Rails application, I encountered an issue with a flash message that appears after successfully completing an AJAX action. The message displays "Patient Added" but does not include a way to close it without refreshing the page. To address this, I atte ...

Successful Ajax function utilizing the data-id parameter

A problem I'm facing is with my feed page that loads a separate file, feedLikes.php, for each post on the feed. Currently, whenever I like a post, it updates the likes using Ajax but unfortunately returns me to the top of the feed. Here’s the code s ...

Initiating the initial element in a list when implementing List with map function in React

Is there a way to automatically trigger the onClick function of the first list item displayed when using the map function in React? I want it to show something, like just logging to the console upon triggering. ... import React, { useState } from "re ...

What methods does Google use to catalogue web pages that load dynamically through jQuery?

Similar Query: Does Google crawl AJAX content? In my forum, the URLs follow a specific format for course pages such as COURSE PAGE - http://www.example.com/course/course-feed/course_id/1 Each course page contains numerous questions, with each questio ...

Refreshing dynamically added rows through ajax updates

I'm struggling to clearly articulate my problem. Currently, I have a function that retrieves values from a database using ajax and then dynamically adds each result as a row in a table. This allows users to edit or delete any row as needed. I'm ...

Using JavaScript/jQuery to Set a Timer for Animation with Google Maps Markers

After clicking a marker on a Google map, I've managed to make it bounce with the following code. However, I'm looking for a way to stop the animation after 2 seconds. Is there some kind of timer function that I can incorporate? Here's the ...