Using jQuery to creatively manipulate background elements with dynamic CSS3 transformations

Hey everyone, I'm currently working on a project that involves creating a background effect using two images that move and fade into each other. I'm using transform translate with keyframe animation to achieve this effect.

However, I've run into a problem where the animation works fine the first time, but then the loops get messy and the timing is off. I'm not sure how to fix this issue.

If any of you can help me solve this problem or suggest a way to use jQuery instead of keyframe animation, I would greatly appreciate it.

You can check out a working demo here.

CSS

#wrapper {
  margin: 20px;
  overflow: hidden;
  position: relative;
}

#content {
  background-color: #FC9;
  padding-top: 200px;
  padding-bottom: 200px;
  color: #000;
  text-shadow: 0 0 1px rgba(255, 255, 255, .7);
}

.bg {
  display: block;
  pointer-events: none;
  position: absolute;
  top: 0;
  left: 0;
  opacity: 1;
  -webkit-animation: 6s fader1 infinite linear;
  -moz-animation: 6s fader1 infinite linear;
  animation: 6s fader1 infinite linear;
}

.bg2 {
  display: block;
  pointer-events: none;
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
  -webkit-animation: 6s fader2 infinite linear, 10s faderopacity infinite linear;
  -moz-animation: 6s fader2 infinite linear, 10s faderopacity infinite linear;
  animation: 6s fader2 infinite linear, 10s faderopacity infinite linear;
  -webkit-animation-delay: 5s;
  -moz-animation-delay: 5s;
  animation-delay: 5s;
}

@keyframes fader1 {
  to {
    transform: scale(1.4) translate(-150px, -150px);
  }
  from {
    transform: scale(1.3) translate(0, 0px);
  }
}

@keyframes fader2 {
  to {
    transform: scale(1.4) translate(0, -150px);
  }
  from {
    transform: scale(1.3) translate(-130px, 0);
  }
}

@keyframes faderopacity {
  0% {
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

HTML

<div id="wrapper" class="view">
  <img class="bg" src="http://skrollex.x40.ru/theme-alice/images/bg/THOR/2941950875_2b2447b557_o-edt.jpg" />
  <img class="bg2" src="http://skrollex.x40.ru/theme-alice/images/bg/THOR/2945791126_0e4aff223a_o.jpg" />
  <div id="content" class="container-fluid">
    <div class="row">
      <div class="col-sm-12 col-md-6 text-right">
        <h1 class="">Hello This is the Title</h1>
        <p>Integer ligula ante, posuere et ante quis, eleifend eleifend ipsum. In sed odio mi. </p>
      </div>
    </div>
  </div>
</div>

Answer №1

If you want to enhance your animation, consider doubling the animation time to create more keyframes for better control. You can then manually add in the delays to fine-tune the timing. I faced a similar challenge where I had to start the first animation midway through the sequence.

#wrapper {
margin:20px;
overflow: hidden;
position: relative;
}
#content {
background-color:#FC9;
padding-top:200px;
padding-bottom:200px;
color:#000;
text-shadow:0 0 1px rgba(255,255,255,.7);
}
.bg {
display: block;
pointer-events: none;
position: absolute;
top:0;
left:0;
opacity:1;
-webkit-animation: 12s fader1 infinite linear;
-moz-animation: 12s fader1 infinite linear;
animation: 12s fader1 infinite linear;
}
.bg2 {
display: block;
pointer-events: none;
position: absolute;
top:0;
left:0;
opacity:0;
-webkit-animation: 12s fader2 infinite linear, 12s faderopacity infinite linear;
-moz-animation: 12s fader2 infinite linear, 12s faderopacity infinite linear;
animation: 12s fader2 infinite linear, 12s faderopacity infinite linear;
}
@keyframes fader1 {
0% { transform: scale(1.35); transform: translate(-75px, -75px); }
25% { transform: scale(1.4); transform: translate(-150px, -150px); }
30% { transform: scale(1.4); transform: translate(-150px, -150px); }
40% { transform: scale(1.3); transform: translate(0px, 0px); }
65% { transform: scale(1.3); transform: translate(0px, 0px); }
100% { transform: scale(1.35); transform: translate(-75px, -75px); }
}
@keyframes fader2 {
0% { transform: scale(1.3); translate(-130px, 0); }
15% { transform: scale(1.3); translate(-130px, 0); }
75% { transform: scale(1.4); right: -150px; transform: translate(0, -150px); }
100% { transform: scale(1.4); right: -150px; transform: translate(0, -150px); }
}

@keyframes faderopacity {
0% { opacity:0; }
15% { opacity:0; }
25% { opacity:1; }
65% { opacity:1; }
75% { opacity:0; }
100% { opacity:0; }
}
<div id="wrapper" class="view">
<img class="bg" src="http://skrollex.x40.ru/theme-alice/images/bg/THOR/2941950875_2b2447b557_o-edt.jpg" />
<img class="bg2" src="http://skrollex.x40.ru/theme-alice/images/bg/THOR/2945791126_0e4aff223a_o.jpg" />
<div id="content" class="container-fluid">
<div class="row">
<div class="col-sm-12 col-md-6 text-right">
<h1 class="">Hello This is the Title</h1>
<p>Integer ligula ante, posuere et ante quis, eleifend eleifend ipsum. In sed odio mi. Vivamus dapibus gravida facilisis. In hac habitasse platea dictumst. Aliquam tincidunt ultricies enim sed pellentesque. In in mi in libero laoreet ultricies. Phasellus non metus dolor parturient vitae neque venenatis.</p>
</div>
</div>
</div>
</div>

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

Can a Chrome extension display a webpage in a separate window using only JS, HTML, and CSS?

I am currently working on creating a custom Google Chrome extension. My goal is to have a small window appear when the user clicks on the extension button, without using window.open or traditional pop-ups, to display a specific web page. I have a basic un ...

Is it possible for two event listeners to operate concurrently?

Currently, I am programmatically generating unique ids for my div elements. These divs display a plus button as shown in the code snippet below: <div id="'+random_number+'" class="inc buttontest">+</div><div class="dec buttontest" ...

Utilize JQuery to target all elements except for those within a specified excluded class

I am looking to target all elements within a container that has the class DnnModule-efforityEaloHTML var all = $(".DnnModule-efforityEaloHTML *") <== This method is effective However, I now want to exclude any items with the class nostrip I have atte ...

Utilize jQuery to retrieve data attributes and set them as CSS properties in a key-value format

Looking at the HTML below: <h4 data-padding-top="100" data-padding-left="0" data-text-align="left" data-color="#ffffff">Promotion Two</h4> I aim to use the data attributes as CSS styles for the H4 element. I have attempted to achieve this ...

Interactive quiz program based on object-oriented principles

As I work on developing a quiz app using JavaScript, everything seems to be going well. However, I've encountered an issue with validation where my code is validating the answers twice - once with the correct answer from the previous question and agai ...

Creating a persistent hover effect

Utilizing primarily CSS, along with background images and adjacent selectors, I am creating a page that displays unique human-readable text information when the user hovers over horizontally stacked images. The textual information is presented in a div on ...

How can we have a div stay at the top of the screen when scrolling down, but return to its original position when scrolling back up?

By utilizing this code, a div with position: absolute (top: 46px) on a page will become fixed to the top of the page (top: 0px) once the user scrolls to a certain point (the distance from the div to the top of the page). $(window).scroll(function (e) { ...

Can the footer be adjusted to be full width only?

Is there a way to stretch only the footer on a blogger simple template to full width without altering other elements? I have looked into similar topics, but they mainly focus on WordPress or involve changing multiple elements, which is not suitable for my ...

Would you like to include or delete a class?

I'm working on implementing a tab system where clicking on different links will change the classes of two divs. Essentially, when you click on one link, it should add a "2" to both classes and make the second div visible. Clicking on the other link wo ...

Row in Bootstrap 3 fails to display correctly on medium-sized devices

Having trouble with divs of different sizes? I want a row that shows 3-column wide divs on medium & large screens and 6-column wide divs on small devices. <div class="container"> <div class="row"> <div class="service col-sm-6 col-md-3"& ...

What can I use instead of "display:none" in Javascript or Jquery?

A problem I encountered with my website involves a menu that toggles content visibility based on menu item clicks. The JQuery script responsible for this behavior is shown below: $(document).ready(function() { $("#bioLink").click(function(){ $ ...

Jquery validation is ineffective when it fails to validate

I have implemented real-time jQuery validation for names. It functions correctly in real-time, however, when I attempt to submit the form, it still submits the value even after displaying an error message. Below is the code snippet for the validation: $ ...

Issues with Jquery setTimeout function not functioning as expected

Here is the jQuery code I'm currently working with: $('div#mid_number_of_mail').mouseover(function () { setTimeout(function () { $('div.element_popup' ,this).stop().animate({ opacity : '1' ...

Exploring Ways to Implement Unobtrusive AJAX in ASP.NET Core Razor Pages Prior to Form Submission

I am attempting to prevent the button from being clicked when I send the ajax request. The only thing that seems to be triggered is the console.log. I initially thought that I could achieve this by using the beforeSubmit option in ajax, but Unobtrusive AJA ...

What are some solutions for fixing a jQuery problem?

I'm encountering an error message every time I try to create a menu item, indicating an issue with jQuery. I need assistance in identifying the problem. Even after attempting to use Firebug, I am unable to pinpoint the root of the problem. Whenever ...

Using jQuery to dynamically swap out <tr> tags and all the elements inside of them

In order to enhance user experience, I am looking to automatically fill in certain form elements within a table when a link is clicked. The intention is for the fields to populate with predefined values and then convert into HTML paragraph elements, preven ...

CSS - The Failure of Implementing Multiple Segmented Controls

Recently, I came across a fantastic segmented control on and I am looking to customize it to fit my requirements. However, I encountered an issue where adding multiple controls to the page only affects the first segmented control. Instead of duplicating ...

I am experiencing an issue with setting the border to none using .navbar-toggler

I'm currently utilizing bootstrap 5, and I've encountered an issue with setting the border and outline to none in my CSS file. Specifically, when I apply these properties to the nav-toggler class, it seems to have no effect. .menu-bar .navbar- ...

What is the best way to apply the addClass method to a list element

I've been searching for a solution to this issue for some time now, and while I believed my code was correct, it doesn't appear to be functioning as expected. HTML <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js ...

Enhancing Image Quality with jspdf and Html2Canvas

We are currently utilizing jsPDF and HTML2canvas to create PDFs, however, we have noticed that the image resolution is quite high. Is there a method available to obtain low-resolution images using jquery, javascript, jsPDF, and html2canvas? function addE ...