Div sliding out of view

I'm encountering a slight issue with this template: essentially, my goal is to implement a feature where clicking on a box will expand it while simultaneously sliding the other boxes off-screen. However, instead of just sliding the div off-screen, it completely disappears.

To view what I have done so far, please visit: JSFiddle.

$(function() {
  $(".box").click(function() {
    var isopened = $(this).attr("isopen");
    if (isopened == "true") {
      $(this).css("position", "relative").css("width", $(this).attr("data-ow"));
      $(this).attr("isopen", "false");
    }
    else {
      $(this).attr("data-ow", $(this).css("width"));
      $(this).css("position", "relative").css("width", "40%");
      $(this).attr("isopen", "true");
    }
  });
});
* {
  margin: 0;
  padding: 0;
}
.container {
  width: 100%;
  max-width: 100%;
  max-height: 600px;
  overflow: hidden;
}
.box {
  height: 600px;
  display: block;
  width: 13.33333333%;
  border: 1px solid white;
  background-color: black;
  float: right;
  position: relative;
}
.box:first-of-type {
  width: 29.0%;
  background-color: orange;
}
.box:last-of-type {
  width: 29.0%;
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

My desired outcome is for the clicked box to expand, and only the portion that extends beyond the screen to be hidden rather than the entire div:

https://i.stack.imgur.com/Ps1Gm.png

Answer №1

If you're looking for a solution that doesn't require jQuery/JS, you might find this flexbox layout interesting. It's all done with pure CSS and HTML:

body {
  background-color: black
}
#container {
  display: flex;
  width: 100%;
  height: 50vh;
}
#container > div {
  flex: 1;
  min-width: 0;
  transition:min-width 0.2s ease;
  outline:0;
}
#container > div:focus {
  min-width: 50vw;
}
<div id="container">
  <div tabindex="0" style="background-color:blue"></div>
  <div tabindex="0" style="background-color:orange"></div>
  <div tabindex="0" style="background-color:green"></div>
  <div tabindex="0" style="background-color:white"></div>
  <div tabindex="0" style="background-color:blue"></div>
</div>

The use of tabindex in conjunction with the :focus selector allows for this interactive functionality.

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

Maximizing the Benefits: Unlocking Potential with the WD Calendar's Dropdown

I recently came across this amazing WD calendar project that I'm incorporating into the project I'm currently working on. You can find it at this link: . However, I am facing a small challenge in remaking the "Add New Event" form as I can't ...

Retrieving exclusive npm packages from an npmjs user

I am currently facing a challenge with transferring all of our npm modules from npmjs.com to a new location. The issue is that our modules are saved under a private npm user account, making it difficult for me to programmatically access and consume all the ...

Using JavaScript to extract the metadata of an image from a remote location

Is there a way to extract XMP metadata from a JPEG file using JavaScript? I came across a method for doing it in PHP (How can I read XMP data from a JPG with PHP?) which can be adapted for JavaScript using AJAX. However, the issue arises when trying to acc ...

Consistent validation success with login authentication

I am currently working on a project that involves querying a server for user authentication using C# and ASP.NET, as I am still learning these technologies. Previously, I worked with JSP. The issue I am facing : For some reason, my methods are always re ...

Center the div element and adjust its scale to perfectly fit the screen

I have been struggling to optimize my website for mobile devices by centralizing the content display. When I access it on a mobile device through , there is excessive space below the content, forcing me to pinch zoom in order to read properly. How can I el ...

Sort through data based on specific data fields upon the button click event using JSON and Vue.js

Is there a way to use JSON files in Vue.js to filter data based on category when a button is pressed? I have objects with categories inside the data. portfolio-v1.json { "data":[ { "image_path":"static/products/DEL ...

I am struggling to apply custom CSS styles to the scrollbar within a Card component using MUI react

import React from "react"; import Card from "@mui/material/Card"; import CardActions from "@mui/material/CardActions"; import CardContent from "@mui/material/CardContent"; import CardMedia from "@mui/material/Ca ...

When attempting to run the npm install mathjs command, an error is displayed

Trying to install mathjs using npm but encountering an error: npm install mathjs The error message received is as follows: npm WARN tar TAR_ENTRY_ERROR UNKNOWN: unknown error, write npm WARN tar TAR_ENTRY_ERROR UNKNOWN: unknown error, write npm WARN tar T ...

Displaying a div when a button is clicked (PHP)

Hello everyone, I'm new to posting here so please bear with me if I seem inexperienced. On my website, there is a button that needs to disappear when clicked and be replaced by a form without refreshing the page. I was able to accomplish this using ...

Can you explain the distinction between angular-highcharts and highcharts-angular?

Our team has been utilizing both angular-highcharts and highcharts-angular in various projects. It appears that one functions as a directive while the other serves as a wrapper. I'm seeking clarification on the distinctions between the two and recomme ...

After attempting to retrieve local .HTML content with JQuery ajax for the second time, the JavaScript code within index.html appears to be malfunctioning

My apologies for my limited proficiency in English... Currently, I am engaged in phonegap development where I am attempting to utilize JQuery ajax to retrieve local .HTML content and insert this extracted content into the div with the id="next-page" in in ...

Is it possible to utilize the map method to extract objects that are nested multiple layers deep?

I am working on accessing the change order information in order to compile a list of all the change_order_names. The code I am currently using is yielding the results displayed below. Could someone assist me in understanding how to access the change order ...

Using Node.js to separate applications on the same URL based on different paths

We currently have a server hosting one domain, where we have placed apps separately using specific URL paths. For instance, the front-end of our app is mapped to the main URL (/). Requests to / will be directed to the front-end app, while adding /api in ...

Using JavaScript to detect the Facebook like button on a webpage

I am currently developing an application for Facebook using HTML and Javascript. My goal is to be able to detect when a user clicks the "Like" button on my company's Facebook page without needing to embed a separate "Like" button within my app itself. ...

HTML Brickwork Picture Arrangement

I recently integrated this code into a WordPress theme and encountered an issue where the new images are displaying vertically instead of horizontally. I am seeking assistance in aligning the new images to appear on the green line instead of the red line a ...

What are the drawbacks of utilizing the onClick function in JavaScript?

What is the rationale behind programmers opting not to use onClick function in Javascript? ...

Inquiring about the rationale behind using `jquery.ui.all.css` specifically

I'm curious about the impact of jquery.ui.all.css on Tabs As soon as I remove it, <link rel="stylesheet" href="jquery/dev/themes/base/jquery.ui.all.css"> My tabs stop functioning. Here's my query: What exactly does the jquery.ui.all.cs ...

Creating beautifully formatted PDFs using pdfmake in AngularJS

I have a HTML popup displaying data retrieved from the server, and I am attempting to download this data as a PDF using the pdfmake plugin. While I am able to generate the PDF file, the challenge lies in replicating the HTML page layout in the PDF. Here is ...

Troubleshooting Bootstrap Social Buttons: Background Color and Hovering Challenges

I've been working with Bootstrap to design social buttons for Facebook and LinkedIn. However, I'm facing an issue where setting the background color to white for the buttons doesn't cover them properly. The white background extends beyond th ...

Incapable of composing text using the MUI SearchBar

I'm having trouble with my Material UI search bar - it's not letting me type anything into it. How can I resolve this issue? Despite trying the suggested code snippets from other answers, I keep encountering errors when integrating them into my ...