Removing the hover effect in Angular can be achieved by targeting the specific element

Currently, I am in the process of constructing an angular application and have encountered an issue that I need assistance with.

Here is a snippet of my code:

<div ng-class="{selectThis : item.pick}" ng-click="pickItem(item)" class="item">Item name here<div>

My JavaScript controller looks like this:

$scope.pickItem = function(item) {
    item.pick = true;
}

Within my CSS file:

.item {
    color: red;
    …more css
}

.item:hover {
    color:blue;
    ...more css
}

.selectThis {
    color:blue;
}

While everything appears to be functioning correctly on desktops, the hover effect persists on touch devices when a user touches the div. I am aware that adding a media query could resolve this issue, but I believe there may be a more modern Angular-based solution available. Any suggestions would be greatly appreciated. Thank you!

Answer №1

To tackle this issue using Angular, consider incorporating a class upon detection of touch events:

app.directive('touchClass', function() {
  return {
    restrict: 'A',
    scope: {
      touchClass: '@'
    },
    link: function(scope, element) {   
      element.on('touchstart', function() {
        element.$addClass(scope.touchClass);
      });

      element.on('touchend', function() {
        element.$removeClass(scope.touchClass);
      });
    }
  };
});

You can then apply this directive to any desired element. It will append the touch class while a touch event is active and remove it once the touch ends.

<div ng-class="{selectThis : item.pick}"
     ng-click="pickItem(item)"
     touch-class="touch"
     class="item">
  Item name here
<div>

This class can be used similarly to the hover pseudo selector in CSS:

.item {
  color: red;
  …more css
}

.item.touch {
  color: blue;
  ...more css
}

.selectThis {
  color: blue;
}

Answer №2

You can also incorporate touch events into your code:

<div ng-class=="{ 'touch': touchStart}"
  on-touch
 class="item">Enter item name here<div>

Create a custom directive to manage touch events:

    .directive('onTouch',function() {
        return {
            restrict: 'A',
            link: function ($scope, $elem, $attrs) {
                $elem.on('touchstart',function(e) {
                    $scope.touchStart= true;
                    $scope.$apply();
                });
                $elem.on('touchend',function(e) {
                    $scope.touchStart= false;
                    $scope.$apply();
                });
            }
        }
    });

Note: This code is a rough draft and has not been tested yet. Hopefully it will be useful for you.

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

Converting CSV data into JSON format using NodeJs and integrating it with MongoDB

Here is how my CSV file is structured: "Some Comment here" <Blank Cell> Header1 Header2 Header3 Value1 Value2 Header4 Value3 Value4 I am looking to convert this into JSON format and store it in MongoDB as follows: Obj: { Key1: Header ...

Tips for utilizing Jquery webcam on mobile devices like Android and iPhone

Currently, I am in the process of developing mobile web applications utilizing jquery mobile and HTML 5. I am working on a feature that requires access to the camera. In order to achieve this functionality, I have integrated the following plugin: While ...

Ways to retrieve the ID attribute value of the third ancestor above

How can I retrieve the ID attribute of the 3rd parent element when a link is clicked? Take for example: <div id="First Div"> <div class="class A"> <div class="class B"></div> <div class="class C"></div> &l ...

Adding a div element to a React component with the help of React hooks

I'm currently diving into the world of React and experimenting with creating a todo app to enhance my understanding of React concepts. Here's the scenario I'm trying to implement: The user triggers an event by clicking a button A prompt app ...

Bizarre idiosyncrasy in Internet Explorer 7/8 regarding Javascript

Whenever I access my page in IE, the sliding carousel feature malfunctions. Instead of displaying images in a carousel format, they are shown in a list format. It seems like IE is not reading the javascript for the selector and applying the effect as it sh ...

Submitting a PDF document to the server with PHP Laravel through AJAX

Struggling to send a PDF file via AJAX to my server for handling by a PHP script in a Laravel project. The file doesn't seem to be making it to the server. Despite receiving a 200 response in the network, the server is returning 'file not presen ...

AngularJS is encountering an issue with the callback function, resulting in an error

Currently, I am utilizing the $timeout service in Angular to decrease a variable from 100 to 1 in increments of 1/10 seconds. Although I understand that using the $interval service would be a simpler solution, for this particular scenario, I am focused on ...

How can I effectively transfer parameters to the onSuccess callback function?

In my react-admin project, I'm utilizing an Edit component and I wish to trigger a function upon successful completion. <Edit onSuccess= {onSuccess } {...props}> // properties </Edit> Here's the TypeScript code for the onSuccess fun ...

Display a pop-up video player when a button is clicked to watch multiple videos on a webpage utilizing jQuery/Javascript

I am in need of assistance with integrating multiple embedded YouTube videos (using iframes) that appear on the screen after a user clicks a custom button. While there are solutions available for single videos, I am struggling to make it work for multiple ...

Tips for modifying HTML attributes in AngularJS

I am looking to modify an attribute of a div using AngularJS. While I am familiar with how to do this in jQuery, I am not sure how to achieve it in Angular. Here is the HTML code: <div ng-app="test"> <div ng-controller="cont"> < ...

Currently, my goal is to create a functional copy button through the use of JavaScript

I've been attempting to create a basic copy button using JavaScript, but I keep encountering an error. TypeError: null is not an object (evaluating 'myInp.select') Whenever I click the copy button, My code looks like this: <!DOCTYPE htm ...

The ajax function nestled within a nested forEach loop completes its execution once the inner callback function is triggered

I am encountering a problem with an ajax call that is nested within a forEach loop inside another function. The issue is that the callback of the outer function is being triggered before the inner loop completes, resulting in "staticItemList" not being p ...

Troubleshooting a jQuery Popup/Modal Glitch

Currently, I am in the process of developing a mobile application using HTML/CSS/PHP/jQuery. I am testing it on Android (Gingerbread) and desktop Chrome. One of the features in my app is a modal box that appears centered above the content when a specific ...

Troubleshooting route problems between AngularJS and Wordpress

After delving into AngularJS for a few weeks, I've been gaining valuable insights on Single Page Applications (SPAs). Since Angular is primarily front-end and requires a third-party data source to fetch information from a database, I decided to incorp ...

Achieving the validation with a bold red border

Hi, I'm currently learning React and I've been using regular JavaScript to validate my form. Here's a snippet of how I'm doing it: <TextField label="Title" variant="outlined" si ...

Cons of storing data in local storage

When it comes to storing information, what are the disadvantages of using local storage or cookies within your website's domain compared to storing them on a third party vendor's domain? ...

Combining the power of Angular CLI with Squarespace

Has anyone managed to successfully integrate Angular CLI into a Squarespace website? I've been trying to find a solution to this issue but haven't had any luck. While it's possible to add custom scripts or use CDNs on Squarespace, deploying ...

New Ways to Style the Final Element in a PHP Array - Part 2

So, here's the current challenge I'm facing along with a detailed explanation of what I'm trying to achieve. Initially, following your suggestions worked smoothly; however, things took a drastic turn when I altered the ORDER BY field and int ...

Functional programming and async in action within a while loop implemented in JavaScript

Searching for a way to implement the following code using functional programming techniques. let membersFound = []; // Iterate through 50 or more pages until we find 5 members. while (membersFound.length < 5) { let member = findMember(/* Invoke Sele ...

Real-time data update with Socket io: instantly refresh data on another page upon form submission without having to manually

What is the best way to utilize socket io in a situation where I have two pages open, one displaying a table of existing data and the other featuring a form for users to input new data? I want the table page to automatically update whenever a user submits ...