Display the element when clicked and conceal it when inactive

Currently, I have created a JSFiddle where I am experimenting with showing and hiding text elements using jQuery. The idea is that when a user clicks on any of the "staff" elements, the corresponding text within each class should be displayed. Subsequently, if the user clicks the "staff" element again, the text should hide/fade out.

You can view my progress so far here: http://jsfiddle.net/tJugd/3571/

Below is a snippet of the HTML code:

<div class="slide" style="height:568px;">
    <div class="staff staff-matt" data-hammer="[object Object]">
        <div id="text1"><h1>Lorem Ipsum<h1><p>lorem ipsum dolar<p></div>
    </div>

    <div class="staff staff-shail" data-hammer="[object Object]">
        <div id="text2"><h1>Lorem Ipsum<h1><p>lorem ipsum dolar<p></div>
    </div>

    <div class="staff staff-leah" data-hammer="[object Object]">
        <div id="text3"><h1>Lorem Ipsum<h1><p>lorem ipsum dolar<p></div>
    </div>
</div>

Additionally, below is an excerpt from the CSS styling:

.slide{
    height:568px;
    overflow: hidden;
}
.staff{
    -webkit-user-select: none;
    -webkit-user-drag: none;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    touch-action: none;
    -webkit-transform-origin: 0px 0px 0px;
    opacity: 1;
    -webkit-transform: scale(1, 1);
    width:33%;
    height:568px;
    background:red;
    float: left;
}
.staff-matt{
    background:red;
    box-shadow: rgba(0, 0, 0, 0.298039) 4px 4px 10px 0px;
}
.staff-shail{
    background:white;
    box-shadow: rgba(0, 0, 0, 0.298039) 4px 4px 10px 0px;
}
.staff-leah{
    background:red;
    box-shadow: rgba(0, 0, 0, 0.298039) 4px 4px 10px 0px;
}

#text1, #text3{
    position:relative;
    background-color:white;
    width:50%;
}

Last but not least, here is a glimpse into the JavaScript logic:

$('.staff').click(function(){
    if($(this).hasClass('clicked')){
        $('.staff').animate({width:'33%'});
    } else {
        $('.staff').not(this).animate({width:'0%'});
        $(this).animate({width:'100%'});
    }
    $(this).toggleClass('clicked');
});

Answer №1

$('.team').click(function(){
    if($(this).hasClass('selected')){
        $('.team').finish().animate({width:'33%'}, 0, function() {
          $("[id^=description]").fadeOut()
        });
    } else {
        $('.team').not(this).finish().animate({width:'0%'});
        $(this).finish().animate({width:'100%'}, 0, function() {
          $("[id^=description]").fadeIn()
        });
    }
    $(this).toggleClass('selected');
});

Example on jsfiddle http://jsfiddle.net/sKda6/9873/

Answer №2

To add a fading effect to your code, use the .fadeIn() and .fadeOut() methods on your targeted div with text:

$('.staff').click(function(){
    if($(this).hasClass('clicked')){
        $('.staff').animate({width:'33%'}).find('div').fadeOut(); //This line was added
    } else {
        $('.staff').not(this).animate({width:'0%'});
        $(this).animate({width:'100%'}).find('div').fadeIn(); //This line was added
    }
    $(this).toggleClass('clicked');
});

To hide the text when the page loads, include div.staff div {display: none} in your CSS.

Check out the JS FIDDLE DEMO 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

What is the best way to automatically load and run every JavaScript file within a specific folder?

I am in the process of developing a straightforward content management system using JavaScript. The system relies on plugins that are stored as individual .js files within a designated "modules" folder. At present, I am utilizing jQuery's getScript() ...

Angular's $http.get method allows you to make HTTP

My goal is to display all lists and the tasks associated with each list. In my controller, I have the following code: $http.get('api/list/').success(function (data) { $scope.lists = data; $scope.tasks = data[0].Task; ...

Generate a responsive list with a pop-up feature under each item (using Vue.js)

Currently, I believe that Vue may not be necessary since most tasks can be done using JavaScript and CSS. I am attempting to design a list layout as follows: [A] [B] [C] [D] When an item is clicked, the information about that specific item should ...

Is there a way to assign a value to a select option in Angular 7?

The issue arises when attempting to modify the value, as it consistently displays the initial option selected. Here is the structure of my HTML code: <div class="col-lg-4 col-md-4 col-sm-12 mt-4"> <div class="form-group"& ...

Route-based Dynamic Div ID Retrieval

Can you extract the ID from route.queryparams in Vue.js? <div id="{{this.$route.query.data}}" class="col-md-12>{{data}}</div> ...

Updating the status of a 2D array with N elements in React: A step-by-step guide

Before I dive into the topic, I must acknowledge that I have come across questions similar to this one before but was unable to find a solution on my own. Updating React state as a 2d array? Let's imagine this as my state object state = { graph ...

The postback event continues to trigger even if the OnClientClick function returns false

Is there a way to prevent Button1_Click from firing after a Jquery function returns false in my code snippet below? <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- ...

Arranging data in a JSON array while handling null values in JavaScript

Here is my JSON array: var jData = [ {id: 1, parent: null}, {id: 2, parent: null}, {id: 3, parent: 1}, {id: 4, parent: 2}, {id: 5, parent: 2}, {id: 6, parent: 1}]; I would like to sort it in the following order (first by id then by parent): [ {id: 1 ...

When I open my website in the browser, the bootstrap card-deck design is not being displayed correctly

I encountered a strange issue while designing my website. The code I wrote for the Bootstrap card-deck class in codeply.com was working perfectly fine. However, when I copied the same code into my PyCharm code editor and then pasted the file link into my b ...

Tips for converting binary image data stored in a database and viewing the images on a web browser

I'm currently working on a web application where users can upload images that are then saved as binary data in my MongoDB using Multer. The issue I'm facing now is how to retrieve and display these images on the webpage. When I try to use the img ...

Creating a dynamic traffic light display: A step-by-step guide

//traffic lights:red,yellow,green fill(trafficFirstLightColor) ellipse(315, 40, 20, 20) fill(trafficSecondLightColor) ellipse(315, 40 + 25 * 1, 20, 20) fill(trafficThirdLightColor) ellipse(315, 40 + 25 * 2, 20, 20) //if three ...

Check if the number field in the If statement is empty or contains an excessive number of decimal places, while excluding cases where only zeroes are present in the decimal places

I am currently working with the following line of code: if ($val === "" || ($val.split(".")[1] || "").length > 2) With some assistance from the helpful individuals here, I have managed to implement this code successfully. ...

Ways to alter an array of objects without using a loop

The following code is functioning properly: for(let i=0;i< this.Array.length ; i++){ if(this.Array[i].propertyObject.hasOwnProperty('header')) this.Array[i].ColumnName = this.Array[i].propertyObject.header; } I am int ...

Maintaining the proportions of images in different screen sizes when resizing

I apologize if this question has already been addressed, but I have been unable to find a solution that works for my specific issue. My Gallery consists of a side list of available images in one section, which when clicked changes the image source in anot ...

One webpage with various states managed by the URL conducted

After conducting a thorough search, I find it challenging to identify the precise terminology for my current inquiry. Mainly, my objective is to display a particular page state (showing/hiding a div) based on the URL. On a single page containing three di ...

Is it possible for me to dynamically alter the innerHTML of an input element using

I am working on a small Angular application. One of my components uses innerHTML to display content. <div [innerHTML]="serviceShared.currentMood"></div> However, when I change the value of serviceShared.currentMood in the main component, noth ...

Issue with CSS min-height causing divs to not align properly

My CSS Code includes media queries to adjust the layout: .clearfloat { clear: both; font-size: 1px; height: 0; line-height: 0; } .box-container { width:100%; text-align:center; } .icon-box { width:32%; max-width:500px; ...

execute javascript code found in the html document

Currently, I have a bash script that utilizes curl to download a page. Then, it uses grep and sed to extract javascript within the html block to a file. Following this, I use node to evaluate and leverage the downloaded javascript. Here is an example: cur ...

Positioning columns in Bootstrap with a fixed layout

Is there a way to troubleshoot issues with Bootstrap col? I attempted the following solution, but it's not yielding the desired outcome: <div class="row"> <div class="col-md-6">Content goes here...</div> <div class="col ...

What is the best way to navigate from a button in NextJS to another page that I have built within the same application?

As I work on developing a website for a pet rescue charity using Next.js, I am facing an issue with getting my button or link to function correctly. Every time I try to navigate to another page within my Next.js app, I encounter a 404 error stating "This p ...