Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
How to create a Star icon using jQuery Mobile ?
Next article icon

How to change icon based on zoom level using jQuery ?

Last Updated : 28 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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= "https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js">     </script> </head>  <body>     <h1 style="color:green;">         GeeksforGeeks     </h1>      <h3>         How to change icon based on zoom level using jQuery?     </h3>      <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20230315100901/gfg_img1.png"          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", "https://media.geeksforgeeks.org/wp-content/uploads/20230315100901/gfg_img1.png");             } else if (zLevel === '0.7') {                 $("#icon").attr("src", "https://media.geeksforgeeks.org/wp-content/uploads/20230315100901/gfg_img2.png");             }             else if (zLevel === '0.9') {                 $("#icon").attr("src", "https://media.geeksforgeeks.org/wp-content/uploads/20230315100901/gfg_img3.png");             }             else {                 $("#icon").attr("src", "https://media.geeksforgeeks.org/wp-content/uploads/20230315100900/GFG1.png");             }         }     </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= "https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js">     </script> </head>  <body>      <h1 style="color:green;">         GeeksforGeeks     </h1>      <h3>         How to change icon based on          zoom level using jQuery?     </h3>      <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20230315100901/gfg_img3.png"          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", "https://media.geeksforgeeks.org/wp-content/uploads/20230315100901/gfg_img2.png");             }             else {                 $("#icon").attr("src", "https://media.geeksforgeeks.org/wp-content/uploads/20230315100901/gfg_img1.png");             }         }     </script> </body>  </html> 

Output:

 

Next Article
How to create a Star icon using jQuery Mobile ?
author
harishcarpenter
Improve
Article Tags :
  • Web Technologies
  • JQuery
  • jQuery-Questions

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 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 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 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
  • 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences