user38208
user38208

Reputation: 1094

Show and hide divs after few seconds in jQuery

I have links that when I hover over shows specific text related to that link. I have used the hover function to show and hide the divs.

I would like to also add the feature such that the text changes automatically after a few seconds to show the contents of the next div. i.e is cycles through the divs. Hide div 1, show div 2, hide 2, show div 3 etc. Below is the code that shows how the hover function works. Hover over the links on the right and the text on the left changes:

$(".o-c").hover(function() {
  $('.home-o-c').show();
  $('.home-i-t').hide();
}, function() {
  $('.home-o-c').hide();
  $('.home-i-t').show();
});

$(".c-f").hover(function() {
  $('.home-c-f').show();
  $('.home-i-t').hide();
}, function() {
  $('.home-c-f').hide();
  $('.home-i-t').show();
});

$(".i-c").hover(function() {
  $('.home-i-c').show();
  $('.home-i-t').hide();
}, function() {
  $('.home-i-c').hide();
  $('.home-i-t').show();
});


$(".c-u").hover(function() {
  $('.home-c-u').show();
  $('.home-i-t').hide();
}, function() {
  $('.home-c-u').hide();
  $('.home-i-t').show();
});
.left-fill {
  background: #0000006b;
  height: 100vh;
}

.right-fill {
  background: pink;
  height: 100vh;
}

.vc_col-sm-6 {
  width: 50%;
  float: left;
}

.pivot-nav {
  list-style: none;
  font-family: 'Montserrat';
  text-align: left;
}

.pivot-nav li a {
  font-size: 1.6rem;
  font-weight: 700;
  text-transform: uppercase;
  display: inline-block;
  position: relative;
  color: #fff;
  text-decoration: none;
  line-height: 40px;
}

.pivot-nav li a.default-underline::after,
.pivot-nav li a:hover::after {
  width: 100%;
}

.pivot-nav:hover a.default-underline:not(:hover)::after {
  width: 0;
}

.pivot-nav li a::after {
  bottom: 0;
  content: "";
  display: block;
  height: 4px;
  position: absolute;
  background: #fff;
  transition: width 0.3s ease 0s;
  width: 0;
}

.home-o-c,
.home-c-f,
.home-i-c,
.home-c-u {
  display: none;
}

.our-company {
  clear: both;
  display: block;
  height: 50vh;
}

.cf2 {
  clear: both;
  display: block;
  height: 50vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body>

  <div class="left-fill text-left wpb_column vc_column_container vc_col-sm-6">

    <div class="wpb_wrapper">
      <p class="home-i-t">TEXT One</p>
      <p class="home-o-c">TEXT One</p>
      <p class="home-c-f">TExt for C f.</p>
      <p class="home-i-c">Some more text fo i c.</p>
      <p class="home-c-u">Get in touch </p>

    </div>
  </div>



  <div class="home-fill right-fill text-right wpb_column vc_column_container vc_col-sm-6">

    <div class="wpb_wrapper">
      <ul class="pivot-nav">
        <li class="pivot-nav-item"><a class="o-c default-underline" href="#" data-toggle="my-scrollspy-2">O C</a></li>
        <li class="pivot-nav-item"><a class="c-f" href="#" data-toggle="my-scrollspy-2">C F</a></li>
        <li class="pivot-nav-item"><a class="i-c" href="#" data-toggle="my-scrollspy-2">I C</a></li>
        <li class="pivot-nav-item" data-toggle="my-scrollspy-2"><a class="c-u" href="#">C U</a></li>
      </ul>

    </div>
  </div>



</body>

Upvotes: 1

Views: 178

Answers (1)

Fran&#231;ois Hupp&#233;
Fran&#231;ois Hupp&#233;

Reputation: 2116

You could do something like this:

var homeLinks = ['i-t', 'o-c', 'c-f', 'i-c', 'c-u'];
var currentLink = 0;
var hovered = false;

$(".home-link").hover(function() {
  hovered = true;
  $('.home-'+homeLinks[currentLink]).hide();
  $('[data-hover='+homeLinks[currentLink]+']').toggleClass('default-underline');
  currentLink = homeLinks.indexOf($(this).attr('data-hover'));
  $('[data-hover='+homeLinks[currentLink]+']').toggleClass('default-underline');
  $('.home-'+homeLinks[currentLink]).show();
}, function() {
  hovered = false;
});

var autoNavInterval = setInterval(autoNav, 1000);

function autoNav() {
  if(hovered === false){
    $('.home-'+homeLinks[currentLink]).hide();
    $('[data-hover='+homeLinks[currentLink]+']').toggleClass('default-underline');
    currentLink++;
    if(currentLink >= homeLinks.length){ currentLink = 0; }
    $('[data-hover='+homeLinks[currentLink]+']').toggleClass('default-underline');
    $('.home-'+homeLinks[currentLink]).show();
  }
}
.left-fill {
  background: #0000006b;
  height: 100vh;
}

.right-fill {
  background: pink;
  height: 100vh;
}

.vc_col-sm-6 {
  width: 50%;
  float: left;
}

.pivot-nav {
  list-style: none;
  font-family: 'Montserrat';
  text-align: left;
}

.pivot-nav li a {
  font-size: 1.6rem;
  font-weight: 700;
  text-transform: uppercase;
  display: inline-block;
  position: relative;
  color: #fff;
  text-decoration: none;
  line-height: 40px;
}

.pivot-nav li a.default-underline::after,
.pivot-nav li a:hover::after {
  width: 100%;
}

.pivot-nav:hover a.default-underline:not(:hover)::after {
  width: 0;
}

.pivot-nav li a::after {
  bottom: 0;
  content: "";
  display: block;
  height: 4px;
  position: absolute;
  background: #fff;
  transition: width 0.3s ease 0s;
  width: 0;
}

.home-o-c,
.home-c-f,
.home-i-c,
.home-c-u {
  display: none;
}

.our-company {
  clear: both;
  display: block;
  height: 50vh;
}

.cf2 {
  clear: both;
  display: block;
  height: 50vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="left-fill text-left wpb_column vc_column_container vc_col-sm-6">

    <div class="wpb_wrapper">
      <p class="home-i-t">TEXT One</p>
      <p class="home-o-c">TEXT One</p>
      <p class="home-c-f">TExt for C f.</p>
      <p class="home-i-c">Some more text fo i c.</p>
      <p class="home-c-u">Get in touch </p>

    </div>
  </div>



  <div class="home-fill right-fill text-right wpb_column vc_column_container vc_col-sm-6">

    <div class="wpb_wrapper">
      <ul class="pivot-nav">
        <li class="pivot-nav-item"><a data-hover="o-c" class="home-link" href="#" data-toggle="my-scrollspy-2">O C</a></li>
        <li class="pivot-nav-item"><a data-hover="c-f" class="home-link" href="#" data-toggle="my-scrollspy-2">C F</a></li>
        <li class="pivot-nav-item"><a data-hover="i-c" class="home-link" href="#" data-toggle="my-scrollspy-2">I C</a></li>
        <li class="pivot-nav-item" data-toggle="my-scrollspy-2"><a data-hover="c-u" class="home-link" href="#">C U</a></li>
      </ul>

    </div>
  </div>

Note that I added a data-hover attribute and a .home-link to your links, this allows to simplify the script... I used a setInterval for the auto display feature, each 1 second but you probably want to change that for something a little longer, and I created the hovered var as a flag to know if the mouse is currently over a link, and prevent the autonav to change the displayed text when a link is hovered.

Upvotes: 1

Related Questions