What is the best way to create a smooth sliding effect using CSS?

Need help with creating a transition effect within a bordered div that reveals hidden content when a button is clicked? I'm looking to have the <a> tag displayed after clicking the button, with both the button and text sliding to the left. Here's what has been accomplished so far:

.testparent {
  margin-left: 35%;
}

.test {
  border: 0.5px solid silver;
  width: 100px;
  z-index: 999;
}

button {
  padding: 10px;
  color: white;
  background-color: blue;
  border: 0.5px solid silver;
  height: 40px;
  outline: none;
  transition: all 0.2s;
  width: 65px;
  display: block;
}

button:focus {
  background-color: red;
  transform: translateX(-75px);
}
<html>

<head></head>

<body>
  <div class="testparent">
    <div class="test">
      <button id="button1">Test</button><br>
      <span>Lorem ipsum dolor sit amet, cu omnium equidem quo, id primis nostro atomorum his. Ad mel facer dicam. Ad vix stet perpetua, ex omnes referrentur vel. Sed te omnes doming, mea ea prima iracundia. Eruditi recteque an cum.</span>
      <a href="https://www.google.com">Google</a>
    </div>
  </div>
</body>
</html

Tried using z-index without success.

Answer №1

Since this question is categorized under JavaScript, I think a solution using JS would be appropriate.

To directly target the test <div>, you can assign it an ID and manipulate it with JS.

By utilizing the addEventListener method with a click trigger, clicking on the element with the ID of click-me will toggle the class active for the content and link by using the classList toggle function.

You can position elements outside the container initially by applying a transform of translate(-100%,0). Adjust the values accordingly if working with containers of different sizes or elements that are not 100% width.

To ensure any content overflowing outside the test container remains hidden, add overflow: hidden; to the styles.

The button can be removed if unnecessary as it doesn't affect the functionality in this setup.

let clickable = document.querySelector( "#click-me" );
let content = document.querySelector( "#content" );
let link = document.querySelector( "#link" );

clickable.addEventListener( "click", function() {
  
  content.classList.toggle( "active" );
  link.classList.toggle( "active" );
  
});
.testparent {
  margin-left: 35%;
}

.test {
  border: 0.5px solid silver;
  width: 100px;
  overflow: hidden;
}

button {
  padding: 10px;
  color: white;
  background-color: blue;
  border: 0.5px solid silver;
  height: 40px;
  outline: none;
  width: 65px;
}

#content,
#link {
  transition: transform .8s ease;
}

#link {
  transform: translate(-100%,0);
  display: block;
}

#content.active {
  transform: translate(-100%,0);
}
#link.active {
  transform: translate(0,0);
}
<div class="testparent">

  <div id="click-me" class="test">
  
    <div id="content">
    
      <button id="button1">Test</button><br>

      <span>Lorem ipsum dolor sit amet, cu omnium equidem quo, id primis nostro atomorum his. Ad mel facer dicam. Ad vix stet perpetua, ex omnes referrentur vel. Sed te omnes doming, mea ea prima iracundia. Eruditi recteque an cum.</span>
      
    </div>
  
    <a id="link" href="https://www.google.com">Google</a>
  
  </div>
  
</div>

You may find additional information about the CSS transform property here.

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

Is it possible to include three sorting states in jQuery DataTables: ASC, DESC, and NO_SORT?

When clicking on a column header in jQuery DataTables, the sorting order toggles between ascending (Down Arrow) and descending (Up Arrow). However, I am looking to customize this behavior: 1st-click ascending 2nd-click descending 3rd-click no-sorting 4th- ...

Attempting to alter the appearance of my validation control by implementing a custom style sheet theme

After naming a style sheet theme Basic and applying it to my web.config file, I created a Validator class in the css file. Utilizing the cssClass attribute with the Validator type, I successfully adjusted the font-weight property but struggled to change th ...

What could be causing my issue with the if-else condition not functioning properly?

Why does the code only work for adding and removing styles in Part (else), but not returning the class when clicked again? var navDropDown = document.querySelectorAll('.menu-item-has-children > a'); for (let i = 0; i < navDropDown.length; i ...

Display when moving up and conceal when moving down

Is there a way to make the div appear when scrolling up and down on the page? I'm new to jquery and could use some assistance, please. ...

Creating a sequence of HTTP calls that call upon themselves using RxJs operators

When retrieving data by passing pageIndex (1) and pageSize (500) for each HTTP call, I use the following method: this.demoService.geList(1, 500).subscribe(data => { this.data = data.items; }); If the response contains a property called isMore with ...

The issue code R10, indicating a boot timeout, has arisen as the web process failed to connect to $PORT within 60 seconds of initiating on Heroku platform

After deploying my Node.js WebApp to Heroku, I encountered the following error: 2021-06-01T09:19:42.615419+00:00 heroku[web.1]: State changed from crashed to starting 2021-06-01T09:19:47.259832+00:00 heroku[web.1]: Starting process with command `node app.j ...

Using CSS to overlay a background image on top of a background gradient

Struggling to get both a background gradient and a background image (transparent PNG) to display in my div. No matter what, only the color gradient shows up while the image remains hidden. If I use a solid color instead of a gradient, the image displays ju ...

Having issues with ReactJS - function not functioning properly when invoked within componentDidMount() and componentDidUpdate() lifecycle methods

I have encountered an issue that has left me puzzled, and I'm hoping someone can provide some guidance on how to solve it. The task at hand involves implementing a function that adds .active classes to li elements within a navigation bar if they cont ...

Ways to transition to the subsequent page in Selenium WebDriver following the click of the submit button

https://i.sstatic.net/QWcHm.jpg After successfully automating the process of filling in a textbox and clicking the "proceed with booking" button using a submit() command, I encountered an issue. The HTML code does not provide a URL that can be used in the ...

Issues with fundamental JavaScript client-side code

As a newcomer to the world of javascript and jQuery, I am diving into my first experiment with javascript. My initial focus has been on changing questions by clicking next or previous buttons. The goal is to create a dynamic quiz webpage that updates quest ...

React - Updating the document title upon user navigation away from the current page

I have encountered a challenge and I am seeking guidance on how to resolve it. Throughout our website, all pages have a default title based on our index.html file: <html lang="en"> <head> <title>My Page Title</title> ...

How can I incorporate multiple quality sources into a flowplayer using Angular?

Is there a way to add multiple sources with different qualities like 1080p, 720p etc.? Thank you in advance. flowplayer('#my_player', { src: '../assets/videos/video_1080p.mp4', // title: 'This is just demo&apo ...

AngularJS: Changing color property using toggle when a CSS class is modified

After starting to learn AngularJS, I have been able to find many helpful answers on this forum. However, there is one particular issue that has been causing me frustration. My goal is to dynamically change the color property of an element only when it has ...

Struggling with sending form data to the back end when uploading images in Angular

I've been facing a challenge trying to implement profile picture upload alongside standard text data and sending it all to the backend to create a new user through mongoose. Despite my efforts, using tools like ng-file-upload/angular-file-upload and e ...

Formatting Date and Time in the Gridview of my Asp.net Application

I have been using this format to display the date and time in a grid. The issue I am facing is that I cannot retrieve the exact HH:MM from the database. Even though the database shows 11:11, my grid is displaying 11:03 instead. Here is the value stored in ...

"Utilizing FabricJs with Multiple Canvases to Display the Same Image Repeatedly

Within a loop ranging from 1 to 2, I have created two canvases on the screen and stored them in an object. However, when I load the same two images onto each canvas, only the last canvas displays the images. Why is it not loading on both canvases? Interest ...

What is the best way to make a black background div that perfectly aligns with another div?

When hovering over an image, I want it to fade out to opacity: 0.4, but instead of fading to white, I would like it to fade to black. To achieve this, I decided to change the background color of the body to black so that the fadeout will be towards black. ...

Converting SVG circle animation into textual form - a step-by-step guide!

<svg width="1000" height="100"> <circle id="orange-circle" r="30" cx="50" cy="50" fill="orange" /> <animate xlink:href="#orange-circle" attributeName="cx" from="50" to="900" dur="2s" begin="0s" ...

Having trouble replicating the progressive response from the server in node.js

Hey there, I recently dipped my toes into the world of node.js and decided to give it a shot. Following Ryan Dahl's tutorial (http://www.youtube.com/watch?v=jo_B4LTHi3I) as my starting point, I reached a section around 0:17:00 where he explains how s ...

Tinymce editor does not display icons as expected

I am attempting to create a custom styled list that is editable with tinymce. The list-items are using Material-Check-Icons as bullet-points, which are added as css-pseudo-elements ::before. This setup works well, but when I integrate tinymce (v5) into the ...