0

Sorry if I messed up the title. I am not really sure how to correctly describe my case. I am still a beginner.

Anyway, I am trying to create a simple hover effect over the images that will display a word out of unique Array, change it to another and repeat every .12s - sort of flashing effect.

I will have 8 images at start, which means I will have to create 8 unique arrays.

The problem is that, to make everything work, I had to multiply same function for every individual image/unique class, which to me, seems a little messy, even though it works.

Here is a little example for two containers - hover over the grey areas:

$(window).on("load", function() {
  
  var LP1 = [
  'ui',
  'ux',
  'webdesign',
  'logo',
  'responsive',
  'personal'
  ], i = 0;

  setInterval(function(){
  $('.left-title').fadeOut(0, function(){
  $(this).html(LP1[i=(i+1)%LP1.length]).fadeIn(0);
  });
  }, 120);
  
  var LP2 = [
  'cover',
  'art',
  'music',
  'print',
  'personal'
  ], i = 0;

  setInterval(function(){
  $('.right-title').fadeOut(0, function(){
  $(this).html(LP2[i=(i+1)%LP2.length]).fadeIn(0);
  });
  }, 120);
  
});
* {
  margin: 0;
  padding: 0;
}
body {
  height: 100%;
  width: 100%;
}
.wrap-fixed-real {
  width: calc(100% - 32px);
  height: calc(100% - 32px);
  position: fixed;
  top: 16px;
  left: 16px;
  z-index: 1;
}
.left {
  top: 0;
  position: absolute;
  left: 0px;
  height: 100%;
  width: calc(50% - 8px);
  background-color: #292929;
}
.right{
  top: 0px;
  position: absolute;
  right: 0px;
  height: 100%;
  width: calc(50% - 8px);
  background-color: #292929;
}
.dimming {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  z-index: 2;
  background-color: #000000;
  transition: opacity .24s 0s cubic-bezier(.64,0,.36,1);
}
.left-title {
  position: absolute;
  display: block;
  width: calc(100% - 32px);
  top: 50%;
  transform: translateY(-50%);
  left: 16px;
  z-index: 3;
  opacity: 0;
  font-family: 'Roboto', sans-serif;
  font-weight: normal;
  font-size: 32px;
  text-align: center;
  line-height: 48px;
  color: #ffffff;
  mix-blend-mode: difference;
  transition: opacity .24s 0s cubic-bezier(.64,0,.36,1);
}
.left:hover .dimming {
  opacity: .4;
}
.left:hover .left-title {
  opacity: 1;
}
.right-title {
  position: absolute;
  display: block;
  width: calc(100% - 32px);
  top: 50%;
  transform: translateY(-50%);
  left: 16px;
  z-index: 3;
  opacity: 0;
  font-family: 'Roboto', sans-serif;
  font-weight: normal;
  font-size: 32px;
  text-align: center;
  line-height: 48px;
  color: #ffffff;
  mix-blend-mode: difference;
  transition: opacity .24s 0s cubic-bezier(.64,0,.36,1);
}
.right:hover .dimming {
  opacity: .4;
}
.right:hover .right-title {
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <div class="wrap-fixed-real">
    <div class="left">
      <div class="left-title">LP1</div>
      <div class="dimming"></div>
    </div>
    <div class="right">
      <div class="right-title">LP2</div>
      <div class="dimming"></div>
    </div>
  </div>
</body>

Sorry for the messy code. If it comes to css, I guess I could create 8 different subclasses and 1 unique that will share same styles but I have no clue how to force single function to use different array for every image/unique class. I was doing some research on the internet but I couldn't find an answer. Maybe I am just using wrrong keywords in google, so if anyone could point me in the right direction, it would be nice. Or maybe creating what I want is possible, only by multiplying a function? I am not sure.

Summary: I want every unique Array to be connected with unique class and single function that will make "flashing inscription" effect happen.

There is also one more thing that I am not sure about. The effect that changes text every .12s will be played for 8 different images at a time. Is it going to slow down my website? Maybe in addition I should hide this effect at start, instead of just setting opacity to 0?

1 Answer 1

1

You could create a little jQuery plug-in for this:

$.fn.flashWith = function (LP, delay) {
    setInterval(function(){
        this.fadeOut(0, function(){
            // cycle the given array as you get the first text
            $(this).text(LP.shift(LP.push(LP[0]))).fadeIn(0);
        });
    }.bind(this), delay);
    return this;
};

$(function() {
    $('.left-title').flashWith([
        'ui',
        'ux',
        'webdesign',
        'logo',
        'responsive',
        'personal'
    ], 120);

  $('.right-title').flashWith([
        'cover',
        'art',
        'music',
        'print',
        'personal'
    ], 120);
});
* {
  margin: 0;
  padding: 0;
}
body {
  height: 100%;
  width: 100%;
}
.wrap-fixed-real {
  width: calc(100% - 32px);
  height: calc(100% - 32px);
  position: fixed;
  top: 16px;
  left: 16px;
  z-index: 1;
}
.left {
  top: 0;
  position: absolute;
  left: 0px;
  height: 100%;
  width: calc(50% - 8px);
  background-color: #292929;
}
.right{
  top: 0px;
  position: absolute;
  right: 0px;
  height: 100%;
  width: calc(50% - 8px);
  background-color: #292929;
}
.dimming {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  z-index: 2;
  background-color: #000000;
  transition: opacity .24s 0s cubic-bezier(.64,0,.36,1);
}
.left-title {
  position: absolute;
  display: block;
  width: calc(100% - 32px);
  top: 50%;
  transform: translateY(-50%);
  left: 16px;
  z-index: 3;
  opacity: 0;
  font-family: 'Roboto', sans-serif;
  font-weight: normal;
  font-size: 32px;
  text-align: center;
  line-height: 48px;
  color: #ffffff;
  mix-blend-mode: difference;
  transition: opacity .24s 0s cubic-bezier(.64,0,.36,1);
}
.left:hover .dimming {
  opacity: .4;
}
.left:hover .left-title {
  opacity: 1;
}
.right-title {
  position: absolute;
  display: block;
  width: calc(100% - 32px);
  top: 50%;
  transform: translateY(-50%);
  left: 16px;
  z-index: 3;
  opacity: 0;
  font-family: 'Roboto', sans-serif;
  font-weight: normal;
  font-size: 32px;
  text-align: center;
  line-height: 48px;
  color: #ffffff;
  mix-blend-mode: difference;
  transition: opacity .24s 0s cubic-bezier(.64,0,.36,1);
}
.right:hover .dimming {
  opacity: .4;
}
.right:hover .right-title {
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <div class="wrap-fixed-real">
    <div class="left">
      <div class="left-title">LP1</div>
      <div class="dimming"></div>
    </div>
    <div class="right">
      <div class="right-title">LP2</div>
      <div class="dimming"></div>
    </div>
  </div>
</body>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you good sir for taking your time. I appreciate it. I gotta admit though, I don't entirely understand how it actually works. I will do some research later on. For a now I am going to implement this. Thanks again! :)
You can read about creating plugins (i.e. defining some $.fn.xxxx function) at jquery.com.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.