If you don't want to permanently change the colour on hover, then you could keep it simple and just apply a class on hover, and use it to specify the CSS for your preview.
UPDATE: jQuery-only Solutions Based on your latest comments and edit, you cannot add classes to the CSS. So lets do it using nothing but jQuery!
1. Add CSS styles with jQuery (keeps the flexibility of CSS classes)
The example below still uses the power and flexibility of css classes but without the need to change the HTML or CSS:
- Creates new CSS rules directly in jQuery - this lets you easily set up the alternative styles in CSS for the preview class, e.g.:
$("<style>.preview .product:hover a{color:yellow;}</style>").appendTo("head");
- adds the
preview
class to the container on mouseover
and removes it on mouseout
$("<style> \
.preview .product a { color:green; } \
.preview .product:hover a { color:yellow; } \
</style>").appendTo("head");
$("#container").mouseover(function() {
$("#container").addClass('preview');
}).mouseout(function() {
$("#container").removeClass('preview');
});
.product a { color:blue; }
.product:hover a { color:red; }
/* for demo only */
#container { padding: 10px; background: #f5f5f5;}
#container div { padding: 10px; margin:20px; border: 1px solid grey; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
<div class="product">
<a href="...">hover colour changes when div is hovered</A>
</div>
</div>
2. Save the default colours and restore on mouseout
As I mentioned previously, another option is to use variables to save the original colours, replace them with the temporary colours on mouseover
and restore the original values on mouseout
. As I also mentioned, this means storing variables for all the styles that will be changed and can get unwieldy and difficult to maintain if you have many.
However I've expanded on this below with an example in case it is something you want to try.
// decalre variables to save the default colours
var origColor = $('.product a').css('color');
var origColorHover; // we can't set this until the link is hovered
$(".product").mouseover(function() {
// save hover colour
if (origColorHover == "") origColorHover = $(this).css('color');
// set the temporary hover colours
$('.product a').css('color','green');
$('.product a:hover').css('color','yellow');
}).mouseout(function() {
// reset the original colour after hovering
$('.product a').css('color', origColor);
$('.product a:hover').css('color', origColorHover);
});
.product a { color:blue; }
.product a:hover { color:red; }
/* for demo only */
#container { padding: 10px; background: #f5f5f5;}
#container div { padding: 10px; margin:20px; border: 1px solid grey; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
<div class="product">
<a href="...">hover colour changes when link is hovered</A>
</div>
</div>
PREVIOUS EXAMPLES (might be helpful to other users with similar questions):
3. Use CSS classes for the alternative colours and apply the class on hover
The example below uses CSS classes to set up the alternate styling for the elements, and used jQuery to add the class to a container on mouseover e.g.:
$(".product").mouseover(function() {
$("#container").addClass('preview');
}).mouseout(function() {
$("#container").removeClass('preview');
});
.product a { color:blue; }
.product:hover a { color:red; }
.preview .product a { color:green; }
.preview .product:hover a { color:yellow; }
/* for demo only */
#container div { padding: 10px; margin: 10px; background: lightgrey;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
<DIV CLASS="product">
<A HREF="...">hover colour changes when div is hovered</A>
</DIV>
</div>
Its difficult to say without knowing how your project works, but in general terms this sounds like a more maintainable and adaptable way to apply different template colours anyway?
You can set up a whole different stylesheet for the .preview
and if you wish, and simply applying that class can change multiple styles at once. e.g. the example below applies a new set of styles to all elements, or if you only want to show it on a per-div basis, you apply the class to the div instead.
$("#container").mouseover(function() {
$(this).addClass('preview');
}).mouseout(function() {
$(this).removeClass('preview');
});
.product a { color:blue; }
.product:hover a { color:red; }
.product2 a { color:green; }
.product2 a:hover { color:yellow; }
.preview { text-align: center;}
.preview .product a { color:green; }
.preview .product:hover a { color:orange; }
.preview .product2 a { color:purple; font-weight: bold; }
.preview .product2 a:hover { color:yellow; }
/* for demo only */
#container { padding: 10px; background: #f5f5f5;}
#container div { padding: 10px; margin:10px; border: 1px solid grey; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
<DIV CLASS="product">
<A HREF="...">hover colour changes when container is hovered</A>
</DIV>
<DIV CLASS="product2">
<A HREF="...">hover colour changes when link is hovered</A>
</DIV>
</div>