Show jQuery block and make it fade in

I'm attempting to put together a simple tab system where each tab is supposed to appear using a fadeIn effect.

In the code below, the .field element is initially hidden with CSS. Although the tabs are displayed correctly, the desired fadeIn effect is not happening as expected.

HTML:

<div class="block">
    <div class="controls">
        <a class="one" href="#">First</a>
        <a class="two" href="#">Second</a>
    </div>
    <div class="field field-one active">Field 1</div>
    <div class="field field-two">Field 2</div>
</div>

jQuery:

$('.controls .one').click(function(){
        $('.field').removeClass('active');
        $('.field-one').addClass('active').fadeIn();
});

$('.controls .two').click(function(){
        $('.field').removeClass('active');
        $('.field-two').addClass('active').fadeIn();
});

CSS:

.field{
    display: none;
}

.field.active{
    display: block;
}

Demo:

http://jsfiddle.net/ReyBd/

Answer №1

Why not just utilize hide and fadeIn? Do we actually require the active class?

Take a look at this example link

Answer №2

By applying the active class, the element is immediately displayed as a block due to your CSS rule, preventing it from fading in.

No need to manually add or remove classes because jQuery's fadeIn() method automatically takes care of this by setting inline styles for opacity and display: block, overriding your CSS settings.

Another option is to use the fadeToggle method, which toggles the visibility of the element based on its current state.

To achieve this effect, keep the initial CSS rule .field {display:none;} intact and simply use the following code:

$('.controls .one').click(function(){
     $('.field').fadeToggle();
});

Answer №3

I adjusted the positioning of certain items using absolute to create a fading effect as the tabbed items overlap each other, instead of stacking as they normally would.

In addition, the active class may not be necessary for your current needs, but I have kept it in case you require it for future additions.

Javascript:

$('.field').removeClass('active').hide();
$('.field-one').addClass('active').show();

$('.controls .one').click(function(){
        $('.field').removeClass('active').fadeOut();
        $('.field-one').addClass('active').fadeIn();
});

$('.controls .two').click(function(){
        $('.field').removeClass('active').fadeOut();
        $('.field-two').addClass('active').fadeIn();
});

HTML:

<div class="block">
        <div class="controls">
            <a class="one" href="#">First</a>
            <a class="two" href="#">Second</a>
        </div>
        <div class="container">
        <div class="field field-one active">Field 1</div>
        <div class="field field-two">Field 2</div>
        </div>
    </div>

CSS:

.field{ width: 100px;
    height: 100px;
    background: green;
    position: absolute;
    top: 0;
    left: 0; }

.container { position: relative; }

.field.active{  }

.field-two{ background: yellow; }

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 switch from http to https in a React application?

When performing audits in Chrome, I encountered a net::ERR_EMPTY_RESPONSE error because Lighthouse was not able to consistently load the requested page. Google developers have recommended configuring my server (possibly node.js) to redirect from http to ht ...

Is it possible to efficiently iterate through map keys in axios using Vue.js?

Currently, I am working on Vue.js exercises that I have created. I am successfully populating a table with data from a JSON API, but I have encountered a challenge. The API contains multiple keys with similar names (strIngredient1, strIngredient2, strIngre ...

The horizontal scrolling with overflow-x is not functioning correctly as it is displaying the scrollbar for the vertical

I am perplexed as to why I am unable to scroll horizontally to view the other pink thumbs. Despite adding an overflow of scroll-x to the thumbs container, which should theoretically allow horizontal scrolling, it only seems to scroll vertically. Could som ...

How to align the last item in the bottom row using Flexbox styling

Is it possible to center the last element when resizing the window to a smaller resolution, even though the parent's justify-content parameter is set to space-between? I understand that using center or space-around would achieve the desired result, bu ...

Adjust the Division's Width to be 20% of the Total Page Width Excluding Margins

https://i.sstatic.net/T0nKq.png I need to create a menu page with 5 equal columns, but I'm facing an issue with the layout. Here's my current CSS for the yellow divs: width:20%; height:100vh; background-color: yellow; display:inline-block; In ...

Locate all nodes in neo4j that are connected to nodes matching a specified list of property values of a certain length

To clarify, I am interested in achieving the following (preferably using node/JS): Imagine you have a set of 3 job requirements ('JavaScript', 'PHP', 'MySQL'). In my graph setup, each Person node can be linked to multiple Sk ...

Contrasting createMuiTheme and getMuiTheme

When would you choose to use one over the other? What are the key differences in how they operate? ...

The loading of charts and animations is sluggish on mobile devices

My Chart.js chart starts with 0 values and updates upon clicking submit to load data from an external database. While this works efficiently on a computer browser, the load time is significantly longer when accessing the page on a mobile device. It takes a ...

Troubleshooting: Date picker malfunction in ASP.NET web form with jQuery

I have been struggling to get the date picker working in my asp.net web form. I have tried changing the code multiple times, testing with other examples, and even creating a new page for testing, but it still isn't functioning properly. <%@ Page L ...

Why am I not receiving the expected feedback after submitting a request for a specific parameter in a Node.js and Express Router REST API?

Developed a Node module utilizing the Express router to facilitate the routes for the dishes REST API. Index.js file: const express = require('express'); const http = require('http'); const morgan = require('morgan'); const b ...

What is the best way to activate an input field when a checkbox is selected in JQuery?

I'm encountering an issue with some code I've written. Here is the portion causing trouble: $('#Reserve, #BuyItNowPrice, #featureplate').attr('disabled','disabled'); $('.fake-fieldset .fake-input').css({&a ...

Enhancing State Management with CombineReducers in TypeScript

export const rootReducer = combineReducers({ login: loginReducer, }); Everything is working fine with the current setup, but I encountered an issue when attempting to combine another reducer: export const rootReducer = combineReducers({ login: lo ...

NextJS middleware API receives an uploaded image file form, but the request is undefined

Currently, I'm utilizing NextJS to handle form data processing and database uploads, with a pit stop at the NextJS API middleware for image editing. pages/uploadImage.tsx This is the client-side code handler. ... async function handleImageUpload(imag ...

Guide on displaying a document in react-doc-viewer from a protected API endpoint in either Next.Js or ReactJs

I am looking to display files in my Next.JS web application using a secure API. The API provides the following data: { "name": "Test1.docx", "contentUri": "https://api.mypurecloud.ie/api/v2/downloads/x ...

Troubleshooting problem in AngularJS: $http request causing DOM to not update when calling jQuery function

I'm encountering an issue where the DOM is not updating when I make a $http request in a jQuery function call. Please take a look at the Plunker In my code script.js, for testing purposes, I have $scope.components defined both globally and within a ...

The JavaScript code is unable to locate a specific variable in the file

Experiencing an issue with hasAttr.js where it is unable to locate the variable :jQuery. Assistance would be greatly appreciated. Just beginning my journey on the client side. ...

Significant issue identified with ajax functionality, requiring immediate attention

After executing the code in the console, I expected to see the ajax object with a readyState of 0 (as I aborted the ajax call). However, Chrome is displaying the number 4 in the console instead of 0. This discrepancy is surprising. http://jsfiddle.net/8yC ...

Upon loading, the Carousel is visible instead of being hidden, which is contrary to its intended behavior

Any help would be greatly appreciated as I am struggling with creating a web page featuring tabs for "London, New York, Shanghai". The initial page displayed is the "welcome" page with the other tabs hidden on load. I successfully implemented a carousel f ...

Getting an HTML element by its ID that was passed from the ng-init directive in Angular can be achieved

When I use ng-click, I am able to retrieve the html element by id. However, when I use ng-init, I only get null. Please take a look at my codepen here. HTML Code: <script type="text/javascript"> var memId = "bb7de28f-0f89-4f14-8575-d494203acec7 ...

Exporting headers of an HTML table using Jquery to Excel is not functioning properly

I need assistance with exporting an HTML Table that is dynamically generated based on the data. The table layout is defined before the data is populated, and once the data is queried, rows are added to the table dynamically. <table id="findCntrctTable" ...