Switching images upon hovering in AngularJS

Looking to change images on hover in my Angular app, encountered site peculiarities making CSS solution impractical.

Resorted to using ng-mouseenter and ng-mouseleave for image swapping instead.

landing.jade

img.share-button(src='images/btn_share.png', ng-click='dropdownShareIcons()')
                img.share-icon-top(src='{{ shareIcons[0].orig }}', data-id='0', ng-mouseenter='colorizeIcons($event)', ng-mouseleave='decolorizeIcons($event)')
                img.share-icon-top(src='{{ shareIcons[1].orig }}', data-id='1', ng-mouseenter='colorizeIcons($event)', ng-mouseleave='decolorizeIcons($event)')
                img.share-icon-top(src='{{ shareIcons[2].orig }}', data-id='2', ng-mouseenter='colorizeIcons($event)', ng-mouseleave='decolorizeIcons($event)')

In the controller, an object with image paths and functions to switch them on hover is implemented.

landing.js

$scope.shareIcons = [
        {orig: 'images/follow_weibo_grey.png', hover: 'images/follow_weibo_colored.png'},
        {orig: 'images/follow_wechat_grey.png', hover: 'images/follow_wechat_colored.png'},
        {orig: 'images/follow_youku_grey.png', hover: 'images/follow_youku_colored.png'},
        ]


    $scope.colorizeIcons = function($event) {
        $event.target.src = $scope.shareIcons[$event.target.dataset.id].hover;
    }

    $scope.decolorizeIcons = function($event) {
        $event.target.src = $scope.shareIcons[$event.target.dataset.id].orig;
    }

Performance slowdown observed on production server during image switching. Seeking solutions within Angular or alternative hacks to optimize speed. Avoiding CSS rewrite preferred.

Appreciate any help provided.

Answer №1

Hey there, I faced a similar issue before and the solution that worked for me was preloading the images. It made a significant difference. Consider adding a small piece of JavaScript code to load the images asynchronously at the start of your page. Here's an example:

var imgs = new Array();
function preloadImgs() {
  for (let j = 0; j < preloadImgs.arguments.length; j++) {
    imgs[j] = new Image();
    imgs[j].src = preloadImgs.arguments[j];
  }
}
preloadImgs(
  "http://example.com/images/img1.jpg",
  "http://example.com/images/img2.jpg",
  "http://example.com/images/img3.jpg",
  "http://example.com/images/img4.jpg",
  "http://example.com/images/img5.jpg",
  "http://example.com/images/img6.jpg"
);

Answer №2

One way to potentially improve loading times is by optimizing the sizes of PNG images on your website. There are various batch optimization tools available for this task, and you can find a helpful blog post comparing some of them here: http://www.example.com/best-image-optimization-tools/
It might be worth experimenting with optimizing a few images to see if it makes a noticeable difference in performance.

Best of luck!

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

Manipulating CSS rules using JavaScript/jQuery

My goal is to create a function that generates a grid based on table data. While the function itself seems to be working, I am encountering an issue where the classes applied are not resulting in any style changes. Below is a snippet of my function: $(doc ...

The installation of Protractor using `sudo npm install -g protractor` on Mac resulted

Encountered an error when running sudo npm install -g protractor, seems like the folder/files are missing. Any suggestions on how to resolve this issue? Thank you in advance :) npm ERR! Darwin 15.6.0 npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm ...

What is the preferred method of compiling Sass: utilizing the Live Sass Compiler extension in VS Code, or setting up and running Sass through npm? (Including guidance on transitioning from node-sass to dart-sass)

As I delve into an online course focused on Advanced CSS and Sass, I have noticed that the techniques being taught seem a bit outdated. The course heavily relies on node-sass in its dependencies, which is now considered deprecated. An alternative solution ...

I need assistance in utilizing my PHP variable UserNameID to remove the specific user's row from the database

I want to give users the option to delete their account from the database with a simple button click on my php forum. See the code snippet below: <div class="btn-group"> <button style="width:200px" type="button" class="btn btn-primary"> ...

I must click the web icon in order to open the link with buttons

I am having trouble with the buttons I added for my social media links. When I click on the button, it doesn't direct me to the link unless I click on the icon within the button. <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7. ...

Positioning of buttons within a div

I am currently designing an inspiration post style: https://i.stack.imgur.com/I5J0H.png However, I am encountering some challenges while creating it: The buttons are not rounded when the window size is changed Social icons are not being displayed I am ...

Is there a way to preserve the HTML template code as it is when saving it in a cell?

Looking to store an HTML email template in a Google Sheet cell, but running into formatting issues. The code resembles this example: See sample Email HTML code The problem arises when pasting the complete email template HTML code in a cell. Upon copying ...

PHTML file IIS handler

I was surprised by the lack of online results when searching for a solution to my issue. In my local PHP project, I am encountering problems with PHTML files not being parsed correctly by IIS. The 'phtml' type is not included in the module mappin ...

Capturing jQuery ajax requests in Angular

Within my Angular application, I am utilizing the integrated $.couch API from CouchDB. My goal is to intercept every ajax request, regardless of whether it originates from Angular's $http service or jQuery's $.ajax (which is utilized by $.couch). ...

Angula 5 presents a glitch in its functionality where the on click events fail

I have successfully replicated a screenshot in HTML/CSS. You can view the screenshot here: https://i.stack.imgur.com/9ay9W.jpg To demonstrate the functionality of the screenshot, I created a fiddle. In this fiddle, clicking on the "items waiting" text wil ...

The best approach to incorporating interactive animation in next.js

My vision is to develop a character creation application using next js. The app should empower users to customize the character using sliders and gender selection buttons. The ultimate goal is to have a 2D animated version of the character that dynamicall ...

What is causing the javascript in my svg files not to function when embedded in an html document?

I have the following code for an SVG: <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="470px" height="260px" version="1.1" onload="addEvent ...

Creating a Validation Form using either PHP or JavaScript

I need to create a form with the following columns: fullname, email, mobile, and address. If the visitor fills out the mobile field, they should only be allowed to enter numbers. And if the visitor fills out the email field, they should only be allowed to ...

Title appears to be left aligned instead of centered

My H1 is having trouble centering as I increase the font size. It positions correctly with a smaller size (30px), but when I increase the font, it becomes too low from the center even though text-align:center; is not helping to solve the issue. What adjust ...

I am looking to have two elements positioned next to each other in equal dimensions, such as the reCaptcha feature

Could someone assist me in positioning these elements side by side, each taking up 50% of the screen? I'm unsure if this can be achieved while using the reCaptcha iFrame. <div class="g-recaptcha" data-sitekey="sitekey" style="transform:scale(0.5); ...

What is the best way to achieve this Bootstrap design without copying the content?

Trying to achieve a specific Bootstrap layout without repeating content is my current challenge. Essentially, I aim to divide some content into 2 columns at the sm/md breakpoints and then switch to 3 columns for the lg breakpoint. The layout for sm/md bre ...

Colorbox: Display a specific section from a separate HTML page

Currently, I am facing a challenge where I need to open just one specific part of an external HTML page in a colorbox. I attempted the following approach, however it is opening the entire page instead of just the specified part (at the div with id="conten ...

The text stubbornly refusing to conform to a single line

I am currently experiencing an issue where the text inside the anchor tag is not adjusting to a single line. Below is my HTML code: <html> <head> <link rel="stylesheet" href="style5.css" type="text/css"> </head> ...

The select2 plugin seems to be having trouble recognizing the Li click functionality

I have a select menu that is using the select2 plugin. I am trying to add a class to the <select> element when a menu option is clicked, but it doesn't seem to be working as expected even after testing with an alert. https://jsfiddle.net/ysm4qh ...

Designs for Creating Webpages

As a beginner in the coding world, I have little experience with HTML and Python. My goal is to create a personal webpage, starting with a template and customizing it to fit my needs. Any suggestions on where to find free templates or where to begin my s ...