jQuery hover effect for ::after

喜你入骨 提交于 2020-12-13 04:03:32

问题


I'm firing several effects when I hover over a div. The problem is that the div also has the pseudo element ::after, which populates the div with a virtual element (a play button) using the content CSS rule.

My hover effects work when I'm hovering any part of the div other than the space where the ::after element is.

Simply, I want to know if there is a way to point towards the ::after element using jQuery. I've tried to define the ::after element as a variable named "play", but have had no luck there either. This is my script:

var play = $('a.image-wrap::after');

$(".image-holder, .big-headline a, .small-headline a, play").on({mouseenter: function () {
      $('.image-holder').css('background-color', color);
      $('.image-holder img').css({
        'mix-blend-mode': 'multiply',
        opacity: .6
      });
      if ($(window).width() > 1115) {
        $('.read').css('right', '35%');
      } else {
        $('.read').css('right', '0');
      }
    },
    mouseleave: function () {
      $('.image-holder').css('background-color', '');
      $('.image-holder img').css({
        'mix-blend-mode': '',
        opacity: ''
      });
      $('.read').css('right', '');
    }
  });

回答1:


What if you just add the new CSS to a style and then remove it when we're done hovering? It works pretty well:

$(".example").on("mouseenter", function () {
	$("#workAround").html(".example:after {background:pink}");
}).on("mouseleave", function () {
	$("#workAround").html("");
});
.example {
	height:10px;
	width:100px;
	background:green;
	margin:20px 0 20px 0;
}

.example:after {
	content:'';
	background:red;
	height:10px;
	width:100%;
  display: block;
	position:relative;
	bottom:-10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<style id="workAround"></style>
<div class="example"></div>


来源:https://stackoverflow.com/questions/42862188/jquery-hover-effect-for-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!