What is the best way to animate a div sliding to the right, as if it is emerging from behind another div?

Alright, so I've got a div with multiple links in it. What I need is for all these links to trigger a sliding animation from the left side to the right within the same div. After my research, it seems like Jquery's animate function should be the way to go. The challenge is that I can't seem to find any examples that demonstrate exactly what I'm looking for. These are the links contained within the div.

#holder {
  width: 100%;
  height: 100%
}
.select {
  font-size: 175%;
  background-color: clear;
  width: 15%;
  padding: 5px;
}
.catselect {
  text-decoration: none
}
#categories {
  font-weight: 800;
  position: absolute;
  top: .25%;
  left: 2%;
}
#myHeader1 {
  position: absolute;
  top: 7%;
  left: 3%;
}
#myHeader2 {
  position: absolute;
  top: 11.5%;
  left: 3%;
}
#myHeader3 {
  position: absolute;
  top: 16%;
  left: 3%;
}
<div id="holder">
  <h1 id="categories">Categories</h1>
  <div class="select">
    <a id="myHeader1" class="catselect" href="#">Comedy</a>
  </div>

  <div class="select">
    <a id="myHeader2" class="catselect" href="#">Horror</a>
  </div>

  <div class="select">
    <a id="myHeader3" class="catselect" href="#">Action</a>
  </div>
</div>

UPDATE My goal is for a separate div to slide to the right of the div containing the links, with this slide being triggered by clicking on the links themselves.

Answer №1

If you're looking for a slide-in effect from the left using CSS, you can achieve this by applying hover styles. Check out this JSFiddle to see it in action.

#holder {
  width: 200px;
  height: 100%;
  background-color: yellow;
  position: relative;
  left: -170px;
  -webkit-transition: left 1s;
  transition: left 1s;
  cursor:pointer;
}
#holder:hover {
  left: 0;
  -webkit-transition: left 1s;
  transition: left 1s
}
.select {
  font-size: 175%;
  background-color: clear;
  width: 15%;
  padding: 5px;
}
.catselect {
  text-decoration: none
}
#categories {
  font-weight: 800;
  top: .25%;
  left: 2%;
}
#myHeader1 {
  top: 7%;
  left: 3%;
}
#myHeader2 {
  top: 11.5%;
  left: 3%;
}
#myHeader3 {
  top: 16%;
  left: 3%;
}
<div id="holder">
  <h1 id="categories">Categories</h1>

  <div class="select"> <a id="myHeader1" class="catselect" href="#">Comedy</a>

  </div>
  <div class="select"> <a id="myHeader2" class="catselect" href="#r">Horror</a>

  </div>
  <div class="select"> <a id="myHeader3" class="catselect" href="#">Action</a>

  </div>
</div>

Based on a comment, an updated version is available:

To achieve a more dynamic sliding effect as requested, take a look at this updated JSFiddle. By combining both CSS and JavaScript, you can create a seamless sliding animation when interacting with different links.

$('.catselect').on('click', function(e){
e.preventDefault();
    var type = $(this).attr('href');
    $('.data-div').removeClass('slide-in').addClass('slide-out');
$(type).removeClass('slide-out').addClass('slide-in');
});
.menu-icon {
    width:30px;
    height:30px;
    background-image:url('https://cdn2.iconfinder.com/data/icons/ui-icon-part-2/128/menu_hamburger-128.png');
    background-size:30px 30px;
    display:inline-block;
    position:relative;
    left:10px;
    top:5px;
    cursor:pointer;
}
#holder {
    border-right:10px maroon solid;
    width: 200px;
    height: 100%;
    background-color:#999;
    position:relative;
    left:-170px;
    -webkit-transition: left 1s;
    transition: left 1s;
    z-index:10;
    box-shadow:1px 3px 5px rgba(0, 0, 0, 0.7);
}
#holder:hover {
    left:0;
    -webkit-transition: left 1s;
    transition: left 1s
}
#holder h1 {
    margin-left:10px;
}
#categories {
    font-weight: 800;
    top: .25%;
    left: 2%;
}
#holder .select {
    width:100%;
    margin-right:5px;
    font-size: 175%;
    border-bottom:dotted #888 1px;
}
#holder .select a {
    color:#222;
    left: 3%;
    text-align:center;
    display:block;
    padding:3px 0;
    text-decoration:none;
}
#holder .select a:hover {
    color:#E0E0E0;
    background-color:maroon;
}
.data-div {
    width:calc(100% - 80px);
    height:auto;
    padding:5px;
    position:absolute;
    top:22px; /* enter this manually because it depends on the #holder menu height */
    left:-100%;
    transition: left 1s ease-in;
    background-color:#EEE;
}
.data-div h2{text-align:center;}
.slide-in{
    left:50px;
    -webkit-transition:left .7s ease-in;
    transition:left .7s ease-in;
}
.slide-out{
    left:-100%;
    -webkit-transition:left .5s ease-out;
    transition:left .5s ease-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="holder">
     <h1 id="categories">Categories<i class="menu-icon"></i></h1>

    <div class="select"> <a class="catselect" href="#comedy">Comedy</a>

    </div>
    <div class="select"> <a class="catselect" href="#horror">Horror</a>

    </div>
    <div class="select"> <a class="catselect" href="#action">Action</a>

    </div>
</div>
<div id="data-container"></div>

Answer №2

To achieve the desired effect, you need to adjust the left property of an element with a transition.

$(document).ready(function(){
    $('#divClickMe').on("click",function(){
        if(parseInt($('#divSideAd').css('left')) < 0){
            $('#divSideAd').css('left','50px');
            $('#divClickButton').addClass('invisibleField');
        }
        else{
            $('#divSideAd').css('left','-270px');
            $('#divClickButton').removeClass('invisibleField');
        }
    })
})
.sideDiv{
    position:absolute;
    left: -270px;
    height: 140px;
    width: 300px;
    background:#fff;
    -webkit-box-shadow: 0px 0px 5px 5px cyan;
    border-radius: 10px;
    padding:1%;
    -webkit-transition:2s;
}

.invisibleField{
    visibility:hidden;
    -webkit-transition:0.5s;

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="sideDiv" id="divSideAd">
    <table Id="divClickMe">
        <tr>
            <td style="width:90%"><span>Click Me to Hide</span> </td>
            <td style="width:5%"><div id="divClickButton">C L I C K  M E!</div></td>
        </tr>
    </table>
    
</div>

I trust this information proves useful in your endeavors.

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 time to shake up the location of your Toastr

Recently, I've been working on implementing Toastr from this GitHub repository. While I am able to receive alerts and toasts as intended, I'm running into issues when trying to apply custom options. It seems to only stick to the default settings ...

Can you explain the significance of the symbol "<<<HTML"?

During my code reading, I came across the symbol <<<H.TML. My colleagues mentioned that it is often used to embed HTML within a PHP file. I attempted to gather more information on its usage but could not find much. Could someone please explain ho ...

Preventing child components from re-rendering unnecessarily with React callbacks

I am facing challenges with avoiding excessive re-renders. Check out this simplified example I created: https://codesandbox.io/s/gracious-satoshi-b3p1u?file=/src/App.js When you open the console, you will notice that a message saying "child has been rend ...

Utilize Angular Material to assign the value of a mat-select component based on the FormControlName property

I am using Angular version 7 and have encountered an issue with a form I am creating. The form includes a dropdown select to choose an option, but when the page loads, the pre-defined value is not showing up. This page is specifically designed for editing ...

Flexbox's bootstrap columns display issue on smaller screens with no wrapping

I am currently working on a section of my website where I have applied some CSS to 2 divs and an anchor tag in order to center the content vertically. However, I am facing an issue with the flex style properties when the window size is less than 768px. In ...

Is there a way to prevent the update status function from refreshing when the dropdownlist is changed in MVC?

Refreshing only the corresponding row when clicking on the status update is essential. Prior to that, I have implemented a dropdown onchange function which determines which table rows are displayed. cs.html: <section class="card "> < ...

Create a variable to hold the value of the nested element

What is the method to assign a variable to access the svg element inside the div with an ID of parent1 using Javascript? Here is the HTML structure: <div id="parent1"> <svg.....></svg> </div> And this is the Javascript code to ...

I am searching for a straightforward Rails sample that incorporates Ajax

Currently, I am on a quest to find a solid demonstration of how AJAX and JSON interact with Rails. I have a Rails application that relies on standard forms and I am looking to enhance it with some ajax functionality. Unfortunately, I haven't come acro ...

Unable to retrieve various parameters of getStaticPaths through getStaticProps in Next.js with Apollo and GraphQL

Review the code below to help me troubleshoot an issue I am encountering. I am utilizing Id to identify single content and titles for links. When I console log params.title: console: android-paging-advanced-codelab However, when I attempt to console ...

Prevent access to Express routes until the route has completed its execution

I am currently working on an Express REST-API route that involves various database actions. My goal is to prevent any other calls from being made to the REST-API until the actions within this particular route have been completed. When this route is access ...

In React Native, what is the method for utilizing index.js rather than separate index.ios.js and index.android.js files to create a cross-platform app?

Thank you for the help so far, I am new to React Native, and I'm trying to develop a cross-platform app. Here is my index.js file: import React from 'react'; { Component, View, Text, } from 'react-nativ ...

What is the best way to showcase a bootstrap range slider and ensure the corresponding value is reflected in the input field?

I have implemented the bootstrap 5 range component, but I am encountering some issues Why is the dot not displaying in the center of the line even though I added the min value of 1? Is there a way to display the value of the range in the input field? How ...

Expandable Menu that slides up and down

Here is a link to an Accordion Menu I have created: https://jsfiddle.net/wfr57s57/ This piece of JQuery Code controls the functionality: $(".menu-item").click(function(e) { var allContents = $(".menu-content"); e.preventDefault(); if(allC ...

Execute a single test from a particular test suite using Jest

Within my file named "test-file-1", I have several describes (test suites) with distinct names, each containing tests that may share similar names across different test suites. In order to run a single test suite or test, I enter the following command: n ...

Utilize the `:not` selector in jQuery for event delegation

Experiencing a bit of difficulty. Please review the code snippet below. $(document).on("click", "*:not(.parent-container a)", function(e) { // Execute certain actions if a link inside .parent-container has not been clicked }); We dynamically add .par ...

I'm perplexed as to why my JavaScript code isn't successfully adding data to my database

Recently, I delved into the world of NodeJS and Express. My goal was to utilize Node and Express along with MongoDB to establish a server and APIs for adding data to the database. Specifically, I aimed to write the code in ESmodules syntax for JavaScript. ...

Having trouble viewing the image slider on an HTML webpage

I've been attempting to incorporate an image slider into my personal website on GitHub, but I've encountered some difficulties as the images are not displaying. Interestingly, I can load individual images without using the slider and they show up ...

Pass a string to a function and receive a JSON response

I'm new to working with jquery and ajax. I have a function that retrieves data and I want to access it through ajax. My function: public JsonResult FillSubject(string GroupServiceID) { //string GSID = GroupServiceID.Substring(1, (GroupServiceID. ...

Persistent table header and fixed column in table

Trying to create an HTML table with a fixed header and fixed first two columns, but encountering issues with the table displaying at the top of the page instead of below a div with a yellow background. Looking for a solution similar to the one shown in the ...

What is the best way to differentiate between clicking on a LI (div) element and clicking on an A link?

How can I prevent users from being redirected to a link when clicking on the LI element itself? Currently, when a user clicks on either the LI element or the link inside, they are taken to the linked page. What I want is for users to only be directed to t ...