Execute a JavaScript transition upon clicking following the display block

Can someone assist me with creating a simple transition from right to left when I click on a button?

To better explain, I have set up an example here: https://jsfiddle.net/Waarx/9kjhnwLp/22/

var button1 = document.querySelector('#div1');
button1.addEventListener('click', function(event) {
  document.querySelector('#display2').style.display = "none";
  document.querySelector('#display1').style.display = "block";
});

var button2 = document.querySelector('#div2');
button2.addEventListener('click', function(event) {
  document.querySelector('#display2').style.display = "block";
  document.querySelector('#display1').style.display = "none";
});
.parent {
  width: 800px;
}

.col {
  float: left;
  width: 20%;
}

.col2 {
  width: 70%;
}

.none {
  display: none
}
<div class="parent">
  <div class="col">
    <a href="#" id="div1">Div1</a>
    <a href="#" id="div2">Div2</a>
  </div>
  <div class="col2">
    <div class="none" id="display1">
      display 1
    </div>
    <div class="none" id="display2">
      display 2
    </div>
  </div>
</div>

Your assistance would be greatly appreciated. Thank you.

Answer №1

Initially, you can achieve the same functionality using jQuery in a more concise manner:

$('#div1').click(function(event) {
  $('#display2').hide();
  $('#display1').show();
});

$('#div2').click(function(event) {
  $('#display2').show();
  $('#display1').hide();
});

Additionally, when animating an HTML element, it is essential to adjust its opacity. Using display: none; alone will not provide any animation effects. Employing classes exclusively can streamline your CSS as well. See how this concept works below:

Put it to the test here:

$( document ).ready(function() {
    $('.div1').click(function(event) {
      $('.display2').addClass('none');
      $('.display1').removeClass('none');
    });

    $('.div2').click(function(event) {
      $('.display2').removeClass('none');
      $('.display1').addClass('none');
    });
});
.display1, .display2 {
  transform: translateX(0);
  transition: all 1s ease-in-out;
}
.none {
  transform: translateX(100%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="parent">
  <div class="col">
      <a href="#" class="div1">Div1</a>
      <a href="#" class="div2">Div2</a>
  </div>
  <div class="col2">
    <div class="display1 none">
       display 1
    </div>
    <div class="display2 none">
      display 2
    </div>
  </div>
</div>

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

Position the paper-icon-button so that it is aligned to the top-right corner of the scaffold

Looking for a way to... <core-scaffold id="core_scaffold"> <core-header-panel mode="seamed" id="core_header_panel" navigation flex> <core-toolbar id="core_toolbar"></core-toolbar> <core-menu valueattr="label" ...

Troubleshooting an Ajax request in Google Scripts with a '$' variable bug

I am trying to implement an AJAX Request using the ajax() method. I have created the functions below on scripts.google.com, but encountered the following error: function AjaxCall() { var arr = { City: 'Moscow', Age: 25 }; $.ajax({ u ...

How can I create a CSS animation that fades in text blocks when hovering over an image?

I have been trying to implement CSS3 transitions to create a hover effect where the text and background color fade in when hovering over an image. Despite my attempts with various selectors and transition types, I can't seem to achieve the desired res ...

The function str.split() is dividing the text based on individual letters rather than the specified delimiter

I am facing an issue where the string of tags retrieved from firebase needs to be split by ',' and stored in the data() for rendering. The firebase snapshot data is correctly formatted after splitting when viewed in the console like this: "t ...

Enhance your date with a specific month using MomentJS

I attempted to retrieve only the Saturdays of upcoming months with: var momentDt = moment('2017-03-18').add(1); //or 2 or 3 However, adding 1 results in April 18, 2017, which is a Tuesday. Is there an easier way in moment to achieve this wit ...

What is the best way to pinpoint the origin of the element.style being injected by JavaScript?

I've been tasked with updating a client's WordPress website, but I'm still getting acquainted with the overall structure of the site. When looking at the blog page (), I noticed that posts are displayed within a wrapper labeled #blogleft, a ...

Learn the process of assigning the present date using the jQuery UI date picker tool

I am looking to implement the feature of setting the current date using Angular/Jquery UI date picker. I have created a directive specifically for this purpose which is shown below: app.directive('basicpicker', function() { return { rest ...

Verify that all keys of an object are present as values in an array of objects

Imagine having two entities like the following: const obj1 = {key1: "", key2: "", key3: ""}; const array2 = [ { name: "key1", }] Is there a way to verify if array2 contains an object with the same name as ea ...

Is there a way to convert Firebase JSON into a JavaScript object? If so, what is the method to

I am currently working on using the kimono web scraper in conjunction with Firebase to obtain data stored as JSON. To convert the JSON to XML, I am utilizing a JavaScript library which allows me to create a variable from the JSON file (an example is shown ...

When first accessing the page, the map may not load properly. However, a simple refresh of the page in VueJS should resolve this issue and allow the

After initially loading the page, the map may not work properly in Vue.js. However, refreshing the page fixes this issue. Can anyone provide assistance with this problem? Here is the relevant code: mounted() { this.geolocate(); }, methods: { ...

What are the steps involved in converting a MERN application into a desktop application?

As a beginner in front-end application development, I recently created a MERN application in React with separate frontend and backend parts. Everything is working smoothly, but now I want to convert it into a desktop application that can run independently ...

When transmitting data from the parent component to the child component, the data successfully passes through, yet the view fails to reflect the

I'm facing an issue with passing data from a parent component to a child component using props. Here is the code snippet I'm working with: <div id="root"> <my-component v-bind:account-types='accountTypes'> </my-c ...

The browser is displaying the raw text data of a file rather than the formatted HTML content

As a newbie in the world of webpage development, I am currently working on my very first webpage project. I have written some HTML code using a text editor and saved it as an HTML file. However, when I try to view it in my browser, all I see is plain HTML ...

JavaScript image sorting function fails to sort multiple ID elements that match

Currently, I am working on a project to develop an image sorter using toggle buttons. However, I have encountered an issue where my function is only effective for the first image ID and not any others. Below is the JavaScript function in question: functi ...

Mocking a React component with Jest's MockImplementation

Currently, I am in the process of testing a react component that renders another component. This secondary component makes an API call to fetch data which is then displayed on the screen. My goal is to understand how I can mock this particular component&ap ...

Socket.io crashes when refreshed repeatedly

I have set up a socket.io connection to a secure https server to receive data from JavaScript. Upon refreshing the page, I noticed that the socket is maintaining the connection - confirming this when I log information in the on('connection', func ...

What is the best way to extract information from a JavaScript object using jQuery?

Is there a JavaScript (jQuery) alternative to foreach for iterating over a JSON object and retrieving key/value pairs? ...

An exploration of effortlessly moving elements using webdriver.io - the power of

I have been attempting to utilize the drag and drop method in WebDriver.io, but I am encountering issues. I followed the example for drag & drop on this website: https://www.w3schools.com/html/html5_draganddrop.asp. This functionality is essential for ...

What is the best way to render multiple components from a single file in the App.js?

I am currently in the process of learning React.js and I have a question regarding displaying multiple components in App.js. Specifically, I have two pages named Home.js and About.js. When running the code, if you click on "About Us", only the About Page ...

Displaying a Bootstrap loading spinner on the right side of a list item

I want to position a Bootstrap loading spinner on the right side of a ui li, with the text "Keywords" on the left side of the ui li. My current HTML code is as follows: <ul class="list-group" id="my-list"> <li class="l ...