How to change icon based on zoom level using jQuery ?
Last Updated :
28 Mar, 2023
In this article, we will see how to change icon based on zoom level using jQuery, along with understanding its basic implementation through the examples.
CSS zoom property in jQuery allows us to set the zoom value for a specific element and also allows us to get and set the value of these properties based on specific conditions. Suppose, we have an icon <img> element on our webpage with a zoom level of 40% at the start. This zoom level’s associated icons should be displayed. When we alter the zoom level of <img>, the icon should change to reflect the new zoom level. We will discuss the different approaches using the Jquery methods that change an icon based on the zoom level.
Approach: Our approach is to initially set the zoom level for this <image> element to 40% and then utilize a button to adjust the zoom level of this <img> element. Another function will be invoked depending on the zoom level to update the src property of the <image> element. With the help of jQuery, a CSS property’s value can be set or retrieved using the .css() function, depending on which element it applies to.
Syntax:
// Change the value of a CSS property
// for a specific element.
$("element").css("property", "value");
// Get the value of a CSS property for
// a specific element.
var value_of_property = $("element").css("property");
The .attr() method in jQuery is used to retrieve or modify an attribute’s value for the selected element. It can be used with <a>, <img>, <input>, and others.
Syntax:
// Change the attribute value of selected element.
$("element").attr("attributeName", "attributeValue");
// Get the value of a attribute of selected element.
var attributeValue = $("element").attr("attributeName");
We will explore & use both methods in order to apply and change the CSS and an attribute value of <img> element.
Steps for changing the icon based on the zoom level:
Step 1: Create an HTML page with the <img> element and set the src attribute of this element. Now apply basic styling to it like width and height. Apply the zoom property and initially set it to 0.4 or any other value you want. You may also use the percentage value for zoom.
Step 2: Import the following Jquery CDN link into <head> element:
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js">
</script>
Step 3: Now create multiple button elements and apply on-click events for changing the zoom level of this <img> element using the .css() property.
Step 4: When changing the zoom level call another method that changes the src attribute value of <img> using the .attr() method based on the zoom levels.
Example 1: This example illustrates changing the icon based on different zoom levels using jQuery.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta name = "viewport" content =
"width=device-width, initial-scale=1.0" >
< style >
#icon {
width: 400px;
height: auto;
zoom: 0.4;
}
</ style >
< script src =
</ script >
</ head >
< body >
< h1 style = "color:green;" >
GeeksforGeeks
</ h1 >
< h3 >
How to change icon based on zoom level using jQuery?
</ h3 >
< img src =
alt = "icon"
id = "icon" >< br >< br >
< button onclick = "changeZoomLevel0()" >
ZOOM 40%
</ button >
< button onclick = "changeZoomLevel1()" >
ZOOM 50%
</ button >
< button onclick = "changeZoomLevel2()" >
ZOOM 70%
</ button >
< button onclick = "changeZoomLevel3()" >
ZOOM 90%
</ button >
< script >
function changeZoomLevel0() {
$("#icon").css("zoom", "0.4");
ChangeIcons();
}
function changeZoomLevel1() {
$("#icon").css("zoom", "0.5");
ChangeIcons();
}
function changeZoomLevel2() {
$("#icon").css("zoom", "0.7");
ChangeIcons();
}
function changeZoomLevel3() {
$("#icon").css("zoom", "0.9");
ChangeIcons();
}
let ChangeIcons = () => {
var zLevel = $("#icon").css("zoom");
if (zLevel === '0.5') {
$("#icon").attr("src",
} else if (zLevel === '0.7') {
$("#icon").attr("src",
}
else if (zLevel === '0.9') {
$("#icon").attr("src",
}
else {
$("#icon").attr("src",
}
}
</ script >
</ body >
</ html >
|
Output: In the above example, we have four buttons for changing zoom levels, and when anyone button is clicked the ChangeIcons() is called to change the icon by changing the src attribute of <img> element.
Example 2: In the example, we have applied the necessary properties to the body element rather than adjusting the zoom level of a particular element. When the body’s zoom level is changed, the icon changes as well.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta name = "viewport" content =
"width=device-width, initial-scale=1.0" >
< style >
body {
zoom: 1;
}
#icon {
width: 400px;
height: auto;
}
</ style >
< script src =
</ script >
</ head >
< body >
< h1 style = "color:green;" >
GeeksforGeeks
</ h1 >
< h3 >
How to change icon based on
zoom level using jQuery?
</ h3 >
< img src =
alt = "icon"
id = "icon" >< br >< br >
< button onclick = "changeZoomLevel0()" >
BODY ZOOM 60%
</ button >
< button onclick = "changeZoomLevel3()" >
BODYZOOM 90%
</ button >
< script >
function changeZoomLevel0() {
$("body").css("zoom", "0.6");
ChangeIcons();
}
function changeZoomLevel3() {
$("body").css("zoom", "0.9");
ChangeIcons();
}
let ChangeIcons = () => {
var zLevel = $("body").css("zoom");
if (zLevel === '0.6') {
$("#icon").attr("src",
}
else {
$("#icon").attr("src",
}
}
</ script >
</ body >
</ html >
|
Output:

Similar Reads
How to create Bars icon using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making Bars icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ href=
1 min read
How to create a Star icon using jQuery Mobile ?
jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making a Star icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="stylesheet" hr
1 min read
How to change the color of an icon using jQuery ?
In this article, we will see how to change the color of the icon using jQuery. To change the color of the icon, we will use a jquery method. jQuery css() method this method is used to change the styling of a particular selector. This method is also can be used for changing the color of the icon. Fir
2 min read
How to create a Back icon using jQuery Mobile ?
jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making a Back icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="stylesheet" hr
1 min read
How to create Left arrow icon using jQuery Mobile ?
jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making a left arrow icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel=âstyleshe
1 min read
How to make Check icon using jQuery Mobile ?
jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making Check icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="stylesheet" hre
1 min read
How to create a Grid icon using jQuery Mobile ?
jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making a Grid icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="stylesheet" hr
1 min read
How to create an Alert icon using jQuery Mobile?
jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making an Alert icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ
1 min read
How to create a Search icon using jQuery Mobile ?
jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making a Search icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="stylesheet"
1 min read
How to create a Home icon using jQuery Mobile ?
jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making a Home icon using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="stylesheet" hr
1 min read