How can I adjust the opacity of a CSS background image with an animated slider?

Is there a way to adjust the opacity of a CSS background-image animation? My goal is to incorporate a slider for this purpose. This scenario presents just one instance, but I often encounter challenges in repurposing existing images for different tasks. How can such situations be handled?

.page-header:after {
  animation: img1 .85s infinite linear;
  background-image: url(https://i.imgur.com/LZJVN0S.png);
  content: "";
  z-index: 100;
  pointer-events: none;
  height: 300%;
  left: -50%;
  opacity: 1;
  position: fixed;
  top: -110%;
  width: 300%;
}

@keyframes img1 {
  0% {
    background-position: top 150px left 120px, 110px 90px;
  }
  50% {
    background-position: top 150px left 100px, 362px 90px;
  }
  100% {
    background-position: top 150px left 120px, 615px 90px;
  }
}
<div class="page-header"></div>
<div style="position:absolute; top:0px; left:0px;"></div>

var rangeVal = document.getElementById('rangevalue');
var range = document.getElementById('range');
var updateVal = function(val) {
  rangeVal.innerHTML = val;
  rangeVal.style.left = val + '%';
};
updateVal(range.value);

var imgP = document.getElementsByClassName("imgP")[0];
range.addEventListener("input", function(value) {
  imgP.style.opacity = this.value / this.max;
});
.imgP {
  opacity: 1;
}
<div class="range" style="position:absolute; top:10px; left:20px">
  <input type="range" id="range" value="100" min="0" max="100" step="1" oninput="updateVal(this.value)" style="position:fixed; top:32px; left:8px">
  <div id="rangevalue" class="range-thumb"></div>
  <img class="imgP" height="imageheight" width="imagewidth" src='https://png.icons8.com/nolan/50/000000/percentage.png' style="position:fixed; top:56px; left:18px">

Answer №1

To implement the desired effect, include .imgP within .page-header in the HTML file. Alternatively, you can substitute imgP with page-hader in the JavaScript code.

var rangeVal = document.getElementById('rangevalue');
var range = document.getElementById('range');
var updateVal = function(val) {
  rangeVal.innerHTML = val;
  rangeVal.style.left = val + '%';
};
updateVal(range.value);

var imgP = document.getElementsByClassName("imgP")[0];
range.addEventListener("input", function(value) {
  imgP.style.opacity = this.value / this.max;
});
.imgP {
  opacity: 1;
}

.page-header:after {
  animation: img1 .85s infinite linear;
  background-image: url(https://i.imgur.com/LZJVN0S.png);
  content: "";
  z-index: 100;
  pointer-events: none;
  height: 300%;
  left: -50%;
  opacity: 1;
  position: fixed;
  top: -110%;
  width: 300%;
}

@keyframes img1 {
  0% {
    background-position: top 150px left 120px, 110px 90px;
  }
  50% {
    background-position: top 150px left 100px, 362px 90px;
  }
  100% {
    background-position: top 150px left 120px, 615px 90px;
  }
}
<div class="page-header imgP"></div>
<div style="position:absolute; top:0px; left:0px;"></div>

<div class="range" style="position:absolute; top:10px; left:20px">
  <input type="range" id="range" value="100" min="0" max="100" step="1" oninput="updateVal(this.value)" style="position:fixed; top:32px; left:8px">
  <div id="rangevalue" class="range-thumb"></div>

Answer №2

let sliderValue = document.getElementById('slidervalue');
let rangeSlider = document.getElementById('slider');
let updateSliderValue = function(val) {
  sliderValue.innerHTML = val;
    sliderValue.style.left = val + '%';
};
updateSliderValue(rangeSlider.value);

let imagePreview = document.getElementsByClassName("page-header")[0];
rangeSlider.addEventListener("input", function(value) {
    imagePreview.style.opacity = this.value / this.max;
});
.page-header:after {
  animation:imageAnimation .85s infinite linear;
  background-image: url(https://i.imgur.com/LZJVN0S.png);
  content: ""; z-index:100; pointer-events:none; height:300%;
  left:-50%; opacity:1; position:fixed; top: -110%; width: 300%;
  opacity:1;
}
@keyframes imageAnimation {
  0% {background-position: top 150px left 120px, 110px 90px;}
  50% {background-position: top 150px left 100px, 362px 90px;}
  100% {background-position: top 150px left 120px, 615px 90px;}
<div class="slider"
     style="position:absolute; top:10px; left:20px;bottom:20">
<input type="range" id="slider" value="100" min="0" max="100" step="1" oninput="updateSliderValue(this.value)"
     style="position:fixed; top:32px; left:8px">
<div id="slidervalue" class="slider-thumb"></div>
<div class="page-header"></div> 
<div style="position:absolute; top:0px; left:0px;"></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

Adding Measurement Units to the Values in RoundSlider: A Step-by-Step Guide

I'm working on a roundslider widget and I want to display units with the values. If the value is between 0 and 999, it should show "Hz" next to it. For values between 1000 and 999999, "KHz" should be displayed, and so on. Below is the original slider ...

Refreshing a section of a webpage using AJAX and PHP

After creating a RESTful API in PHP, I can easily register information by accessing the address . This registration process involves sending a POST request. Below is an overview of my method: File: api.php <?php /* File: api.php */ private function ...

What could be causing the issues with SSL certificates when using Node.js/Express-TypeScript?

I'm currently in the process of transitioning a project's backend from JavaScript (Node.js/Express) to TypeScript. However, I've encountered an unusual issue where FS's readFileSync is unable to access the key.pem or cert.pem files in t ...

Comparison: executing an immediately invoked function expression (IIFE) and a separate

During my recent refactoring of some legacy code, I stumbled upon the following example: // within another function const isTriggerValid = await (async () => { try { const triggers = await db.any(getTriggerBeforeBook, { param ...

Ways to ensure a div is positioned beneath another div

I am facing an issue with two divs; one on the right and the other on the left. When I try to add a third div below the right div, it ends up appearing under the left div or at the left side of the left div. Here is my current setup: https://i.sstatic.n ...

Having trouble displaying dynamically added images in my jsx-react component. The images are not showing up as expected

import React from "react"; import ReactDOM from "react-dom"; var bImg = prompt("Enter the URL of the image you want to set as the background:"); const bStyle = { backgroundImage: "url(bImg)"; // The link is stored ...

Trouble with CSS table background display in Outlook

I am experiencing issues with an HTML table in an email template: <table id="x_formHeaderTable" style="width:100%; margin:0; padding:0; background-color:#FCE68D; border-style: solid; border-color: #000000;"> <tbody> <tr> &l ...

[SCSS] I am experiencing difficulties with loading fonts on my machine/browser, while they are functioning normally on other devices

When using relative paths, fonts may not load properly. However, absolute paths seem to work without any issues. //styles.scss @font-face { font-family: 'Roboto-Bold'; src: url('../fonts/Roboto-Bold.ttf'); } @font-face { font-fam ...

Firefox 3.5: The Notorious Code Copycat

Encountering an issue in Firefox 3.5.2 where after clicking a link on the page and then returning using the back button or another link, extra HTML code appears. Unsure if it's added by Firefox or related to PHP code. Unfortunately, unable to share t ...

Is there a way to send JSON data back to Riot.js from a Node.js express server?

I am in the process of creating a web application using Riot.js on the client side and Node.js express on the server side. One of the challenges I'm facing is how to mount JSON data with Riot.js for rendering HTML. Currently, I have JSON data stored o ...

What is the best way to encode only a specific section of a JavaScript object into JSON format?

Currently, I am in the process of developing a 2D gravity simulation game and I am faced with the challenge of implementing save/load functionality. The game involves storing all current planets in an array format. Each planet is depicted by a Body object ...

Problems with select box rendering in Internet Explorer with Materialize CSS

When using materializecss select box in Internet Explorer 9 or higher, scrolling with the mouse button is not working. You must click on the scroll bar inside the select box for it to scroll. This issue does not occur in other browsers. I have attached a s ...

Best practices for incorporating JavaScript into Angular applications

I am looking to integrate a JavaScript library into my Angular application. After researching various methods, I have decided to incorporate the library found at . I was hoping to create a module named 'plot-function' that would offer a function ...

Effective Ways to Transfer Data from Angular to CSS in an HTML Table

I am working on an Angular web app that includes an HTML table. The data for this table is retrieved from a database via a service and displayed as text. One of the columns in the table represents a Status, which I want to visually represent as a colored ...

Activate filtering beyond the AngularJS datatable

Currently, I am experimenting with a codepen to understand how to filter data based on a clicked span element. The table is where the data is displayed and I'm looking for a way to trigger filtering outside of it. While the angularjs documentation spe ...

Using Ajax and PHP to upload an image

I'm looking to implement an image upload feature triggered by a button click with the id of #myid.save. Below is the code I have so far: HTML Code: <canvas id="cnv" width="500" height="100"></canvas> <input id="myid_save" type="submit ...

Is there a way to verify if a user navigated to a specific route or URL using the back button?

What is the best way to implement functionality that triggers when a user returns to a previously visited page in their browsing history? ...

Personalized Navigation Bar Toggle (transitioning from dropdown to sliding from the left)

On larger screens, I would like the navigation/navbar to be positioned at the top. However, on mobile view or when collapsed, I want the toggle button to reveal the navigation menu by sliding from the left side, similar to this example: (source by David B ...

Creating a key-shaped design with CSS: A step-by-step guide

Looking to create a unique shape within an HTML div? By utilizing custom CSS in an HTML table, I can design a layout where one column features a rounded avatar while the other displays a rectangular box. However, I am struggling to make the circle overlap ...

Tips for implementing a dynamic value in the ng-style based on certain conditions

Is there a way to set a background-color conditionally using ng-style when the color value can be updated from $scope? These are the variables in my scope: $scope.someone = { id: '1', name: 'John', color: '#42a1cd' }; $scope ...