Fade in and out div onclick with jQuery

I have div

left and right of the page. It is currently div

located on the right hidden

.

I'm trying to use jQuery to fadeout div

which is currently being displayed and replace it with fade in hidden

div

.

What am I doing wrong? I followed the example of this similar question .
This will happen when you click on the "About" link in the code snippet:

$("a").on('click', function() {
  $("#feed-show").fadeIn();
  $(".feed").fadeOut();
});
      

a {
  color: rgba(255, 80, 70, 1) !important;
  text-decoration: none;
}

.nav a {
  font-size: 13px;
  color: rgba(255, 80, 70, 1);
  text-decoration: none;
  font-weight: bold;
}


/* Content ---------------------*/

/* nav */
.nav {
  position: fixed;
  float: left;
  width: 96%;
  left: 2%;
  margin-left: -2px;
  border-bottom: 2px solid rgba(255, 80, 70, 1);
  padding-bottom: 18px;
  background: white;
  z-index: 999;
  top: 0px;
  padding-top: 18px;
}

.c1 {
  max-width: 24%;
}

.column {
  position: relative;
  float: left;
  padding-right: 1%;
  width: 585px;
}

/* feed */
.feed {
  width: 96%;
  left: 2%;
  margin-top: 75px;
  padding-left: 2px;
}

.c2 {
  max-width: 49%;
}

.feed-item {
  position: relative;
  width: 100%;
  height: auto;
  padding-bottom: 25px;
  padding-top: 2.5%;
}

.feed-show {
  position: absolute !important;
  top: -9999px !important;
  left: -9999px !important;
  background: red;
}
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="nav">
  <div class="column c1">
    <a href="#" rel="click">About</a>
  </div>
</div>

<div id="feed" class="feed" style="margin-top: 54px;">
  <div class="column c2">
    <p>
      Creatives of Colour (C-oC) is an independent directory that provides you with up to date information on current, and future work of creatives of colour being showcased in the UK. C-oC aims to contribute to the necessary exaltation of talented artists
      within the various ethnic minorities within the UK.
    </p>
    <p>
      <a href="#">Find out more about Creatives of Colour..</a>
    </p>
  </div>

  <!-- Show on click -->

  <div id="feed-show" class="feed-show" style="margin-top: 54px;">
    <div class="column c2">
      <p>
        Creatives of Colour (C-oC) is an independent directory that provides you with up to date information on current, and future work of creatives of colour being showcased in the UK.
      </p>
      <p>
        <a href="#">Find out more about Creatives of Colour..</a>
      </p>
    </div>
  </div>
      

Run codeHide result


Here is the codepen

Many thanks!

+3


source to share


2 answers


I made some changes to your code:

I changed position: absolute to hide # show transmission with display: none

I changed inside the fadeIn click function to execute when fadeOut completes to avoid the weird moving effect



$(document).ready(function(){
  $("#feed-show").fadeOut(0);
  $("a").on('click', function() {
  
     $(".feed").fadeOut(1000,function(){
        $("#feed-show").fadeIn(1000);
     });
  });
});
      

    a {
    color: rgba(255,80,70,1) !important ;
    text-decoration: none;
}

.nav a {
    font-size: 13px;
    color: rgba(255,80,70,1);
    text-decoration: none;
    font-weight: bold;
}


/* Content ---------------------*/

/* nav */
.nav {
    position: fixed;
    float:left;
    width: 96%;
    left: 2%;
    margin-left: -2px;
    border-bottom: 2px solid rgba(255,80,70,1);
    padding-bottom: 18px;
    background: white;
    z-index: 999;
    top: 0px;
    padding-top: 18px;
}

.c1 {
    max-width: 24%;
}

.column {
    position: relative;
    float:left;
    padding-right: 1%;
    width: 585px;
}

/* feed */
.feed {
    width: 96%;
    left: 2%;
    margin-top: 75px;
    padding-left: 2px;
}

.c2 {
    max-width: 49%;
}

.feed-item {
    position: relative;
    width: 100%;
    height: auto;
    padding-bottom: 25px;
    padding-top:2.5%;
}
#feed-show{
  display:none;
}
#feed-show p{

   background:red;
}
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="nav">
            <div class="column c1">
                <a href="#" rel="click">About</a>
            </div>

        </div>
<div id="container">
        <div id="feed" class="feed" style="margin-top: 54px;">

            <div class="column c2">
                <p>
                    Creatives of Colour (C-oC) is an independent directory
                    that provides you with up to date information on
                    current, and future work of creatives of colour
                    being showcased in the UK. C-oC aims to contribute to the
                    necessary exaltation of talented artists within the various
                    ethnic minorities within the UK.
                </p>
                <p>
                    <a href="#">Find out more about Creatives of Colour..</a>
                </p>
            </div>
			</div>
    <!-- Show on click -->

    <div id="feed-show" class="feed-show" style="margin-top: 54px;">

            <div class="column c2">
                <p>
							OCULT TEXT
                    Creatives of Colour (C-oC) is an independent directory
                    that provides you with up to date information on
                    current, and future work of creatives of colour
                    being showcased in the UK.
                </p>
                <p>
                    <a href="#">Find out more about Creatives of Colour..</a>
                </p>
            </div>


        </div>
 </div>
      

Run codeHide result


+2


source


You have some problems with your code.

First, your div feed-show

is inside your div feed

. So if you are fadeOut()

your feed

div, everything inside will be hidden.

And secondly, in your CSS, you have an absolute position and top and left properties set to yours feed-show

, so even if you are fadeIn()

that element, you won't be able to see it.

I made a couple of changes to your code, so you can see one div disappear and the other disappear.



Hooray!

$("a").on('click', function() {
   $("#feed-show").fadeIn();
   $(".feed").fadeOut();
});
      

a {
  color: rgba(255,80,70,1) !important ;
  text-decoration: none;
}

.nav a {
  font-size: 13px;
  color: rgba(255,80,70,1);
  text-decoration: none;
  font-weight: bold;
}


/* Content ---------------------*/

/* nav */
.nav {
  position: fixed;
  float:left;
  width: 96%;
  left: 2%;
  margin-left: -2px;
  border-bottom: 2px solid rgba(255,80,70,1);
  padding-bottom: 18px;
  background: white;
  z-index: 999;
  top: 0px;
  padding-top: 18px;
}

.c1 {
  max-width: 24%;
}

.column {
  position: relative;
  float:left;
  padding-right: 1%;
  width: 585px;
}

/* feed */
.feed {
  width: 96%;
  left: 2%;
  margin-top: 75px;
  padding-left: 2px;
}

.c2 {
  max-width: 49%;
}

.feed-item {
  position: relative;
  width: 100%;
  height: auto;
  padding-bottom: 25px;
  padding-top:2.5%;
}

.feed-show {
  display: none;
  background:red;
}
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="nav">
  <div class="column c1">
    <a href="#" rel="click">About</a>
  </div>
</div>

<div id="feed" class="feed" style="margin-top: 54px;">
  <div class="column c2">
    <p>
      Creatives of Colour (C-oC) is an independent directory that provides you with up to date information on current, and future work of creatives of colour being showcased in the UK. C-oC aims to contribute to the necessary exaltation of talented artists
      within the various ethnic minorities within the UK.
    </p>
    <p>
      <a href="#">Find out more about Creatives of Colour..</a>
    </p>
  </div>
</div>

<!-- Show on click -->
<div id="feed-show" class="feed-show" style="margin-top: 54px;">
  <div class="column c2">
    <p>
      Creatives of Colour (C-oC) is an independent directory that provides you with up to date information on current, and future work of creatives of colour being showcased in the UK.
    </p>
    <p>
      <a href="#">Find out more about Creatives of Colour..</a>
    </p>
  </div>
</div>
      

Run codeHide result


+2


source







All Articles