Change div with multiple images on hover - html

I have div with multiple images that are static.
<div class="logos">
<img src="img/logos/imgo.png">
<img src="img/logos/imgo1.png">
<img src="img/logos/imgo2.png">
<img src="img/logos/imgo3.png">
</div>
What I want to achieve is when I hover on some image to be changed with another image. How can be done this?
If it was just one image I know that I can make like this:
.logos:hover {
background-image: url('img/logos/another-image.png');
}
but with multiple i don't know.

I guess you can do this with jQuery
<div class="logos">
<img data-hoverimg="img/logos/hoverimgo.png" src="img/logos/imgo.png">
<img data-hoverimg="img/logos/hoverimgo1.png" src="img/logos/imgo1.png">
<img data-hoverimg="img/logos/hoverimgo2.png" src="img/logos/imgo2.png">
<img data-hoverimg="img/logos/hoverimgo3.png" src="img/logos/imgo3.png">
</div>
and place the jQuery in ready function
jQuery('.logos img').hover(function(){
var static_src = jQuery(this).attr('src');
jQuery(this).attr('src', jQuery(this).data('hoverimg'));
jQuery(this).data('hoverImg', static_src);
}, function(){
var static_src = jQuery(this).attr('src');
jQuery(this).attr('src', jQuery(this).data('hoverimg'));
jQuery(this).data('hoverImg', static_src);
});

You could create ids for your imgs :
<img id="myimg1" src="img/logos/imgo.png">
<img id="myimg2" src="img/logos/imgo1.png">
<img id="myimg3" src="img/logos/imgo2.png">
<img id="myimg4" src="img/logos/imgo3.png">
And for the CSS :
#myimg1:hover {
background-image: url('img/logos/another-image1.png');
}
#myimg2:hover {
background-image: url('img/logos/another-image2.png');
}

Related

How allow to select an image in html and visualize it as main while the remaining pictures stay as thumbnails?

I have the following snippet of code in html:
<div class="row">
<div class="col-sm-6">
<div class="product-images">
<div class="product-main-img">
<img src="img/product-thumb-13.jpg" alt="">
</div>
<div class="product-gallery">
<img src="img/product-thumb-14.jpg" alt="">
<img src="img/product-thumb-75.jpg" alt="">
<img src="img/product-thumb-76.jpg" alt="">
</div>
</div>
</div>
</div>
I would like to allow users of the website to click on one of the three thumbnails (14,75 and 76) in the "product-gallery" class and allow it to be visualized as main image (like product 13), hence in bigger size, while the other 3 stay smaller.
The process should be applicable to anyone of the three images on click (typical visualization of products in an ecommerce website, where a particular image is made bigger on click and the others stay small).
I have looked online for applicable methods but cannot find the one I am looking for.
Thanks for the help, have a good day!
This can be achieved by adding an onClick event handler for all the secondary images to change the src attribute of the central image. I have used specific ids to select the images from the DOM. Then we flip the image sources.
<div class="row">
<div class="col-sm-6">
<div class="product-images">
<div class="product-main-img">
<img id="primary-img" src="img/product-thumb-13.jpg" alt="">
</div>
<div class="product-gallery">
<img id="secondary-img" src="img/product-thumb-14.jpg" alt="">
<img id="secondary-img" src="img/product-thumb-75.jpg" alt="">
<img id="secondary-img" src="img/product-thumb-76.jpg" alt="">
</div>
</div>
</div>
</div>
window.onload = function(e){
const secondaryImages = document.querySelectorAll("#secondary-img")
const primaryImage = document.getElementById("primary-img")
secondaryImages.forEach((image) => {
image.addEventListener("click", function() {
const backupSrc = primaryImage.src
primaryImage.src = image.src
image.src = backupSrc
})
})
}
EDIT: fixed code bugs (string literals)

How do I make a specific tag appear on click and hide the other one present in a div?

I'm trying to do a simple component that just change the icon img on click, it's two simple images. The problem is that the close icon doesn't even show up and the icon to open doesn't hide when I click on it.
I've tried adding (click)="isVisible = !isVisible" and [ngClass]="{ 'isVisible': isVisible }"
None of these seems to work, here is my HTML:
<div class="toggle" (click)="isVisible = !isVisible">
<div class="icon">
<img src="https://image.flaticon.com/icons/png/512/64/64576.png" class="icon-open" alt>
<img src="https://image.flaticon.com/icons/png/512/24/24139.png" class="icon-close"
[ngClass]="{ 'isVisible': isVisible }" alt>
</div>
</div>
I expect .icon-close to show up and '.icon-open' to hide when I click on .toggle
Here you should use angular's directive *ngIf.
<div class="toggle" (click)="isVisible = !isVisible">
<div class="icon">
<img *ngIf="isVisible" src="https://image.flaticon.com/icons/png/512/64/64576.png" class="icon-open" alt>
<img *ngIf="!isVisible" src="https://image.flaticon.com/icons/png/512/24/24139.png" class="icon-close" alt>
</div>
</div>
I would use a toggle class to toggle between 2 icons. I use something like below:
$().ready(function(){
$(".closeButton").click(function () {
$(".searchContents").slideToggle('1000');
$(this).find('i').toggleClass('fas fa-times fas fa-sort-down')
});
});
As you are using hide and show functionality you need to create a two class for hide and show like.
.block {
display: block;
}
.none {
display: none;
}
and use it like
<div class="toggle" (click)="isVisible = !isVisible">
<div class="icon">
<img src="https://image.flaticon.com/icons/png/512/64/64576.png" height="30px" class="icon-open" [ngClass]="{ 'isVisible': !isVisible }" alt>
<img src="https://image.flaticon.com/icons/png/512/24/24139.png" height="30px" class="icon-close"
[ngClass]="{ 'block': isVisible,'none':!isVisible }" alt>
</div>
please check full demo here Demo

How to access child element of sibling of its parent in html by css

Can anyone tell how I can access a child element of an element which sibling of an element of parent of current attribute?
<div class="views-field views-field-field-mobile-service-new-image">
<div class="field-content">
<img typeof="foaf:Image" src="image1.png" width="55" height="55" alt="" />
</div>
</div>
<div class="views-field views-field-view-node">
<span class="field-content">
<a href="#" class="colorbox-node" data-inner-width="650" data-inner-height="450">
<img typeof="foaf:Image" src="image2.png" width="55" height="55" alt="" />
</a>
</span>
</div>
how I can access image in other div on hover of class field-content which is child of first div
Is it like, when you hover on image of one div at that time the second div image should be replaced by the first div's img?
If i understand you well, you can't do this with the CSS, but you can do it with JS, smth like that:
$('.views-field-view-node img').hover(function() {
var $yourImage = $(this).closest('.views-field-view-node')
.parent().find('.views-field-field-mobile-service-new-image img').addClass('hover');
}, function() {
var $yourImage = $(this).closest('.views-field-view-node')
.parent().find('.views-field-field-mobile-service-new-image img').removeClass('hover');
});
It's only example, you can improve my code and make it better, if you want.

repeating an embedded data URI image

Let's say I have an embedded image in an HTML file using a data URI (e.g. <img src="data:image/png;base64,...." />.
Is there any way to show that image several times, with subsequent images referring to the initial one, without having to repeat the data URI? for example (though a bad one):
<html><head>...</head>
<body>
<p>Here's my car:</p>
<img id="img1" src="data:image/png;base64,..." />
<p>Some details about my car</p>
<p>Did I mention I just bought a car?</p>
<p>And here it is again:</p>
<img something_to_show_another_image_again="#img1" />
...
</body>
</html>
Seems a bit of an odd situation. Usually you would achieve this by having the image data in a separate file and then each image loading that source.
Ideally you would move the data to a separate file but if that's not an option maybe you could use the data-uri as a background image to each element? Failing that, you could use JavaScript to set the source of each image.
The following snippet includes examples of both.
var images = document.querySelectorAll("#JS img");
var src = images[0].src;
for (var i = 0; i < images.length; ++i) {
images[i].src=src;
}
#CSS img {
width: 20px;
height: 20px;
background: url(data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7) center center no-repeat;
}
<div id="CSS">
<p>The image src is empty but the background is defined in the stylesheet:</p>
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
</div>
<hr />
<div id="JS">
<p>Here we use JavaScript to set the source of all these images to match that of the first:</p>
<img src="data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
</div>
Update...
Further to your updated question I've explored a JS solution that seems to meet your requirements (assuming JS is allowed):
(function linkAliasedImages(){
var aliasedImages = document.querySelectorAll("img[src*='#']");
for (var i = 0; i < aliasedImages.length; ++i) {
var aliasID = aliasedImages[i].src;
aliasID = (aliasID.substring(aliasID.indexOf("#")+1));
var source = document.getElementById(aliasID).src;
aliasedImages[i].src=source;
}
})();
<p>Here's my star:</p>
<img id="img1" src="data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7" />
<p>Some details about my star</p>
<p>Did I mention I just bought a star?</p>
<p>And here it is again:</p>
<img src="#img1" />
<p>And again:</p>
<img src="#img1" />
<img style="background: url(URI) repeat-x;width:100px;" />
Width have to be more than you're image width.

Changing images on hover with css/html

I have a problem with changing images by hover effect. Actually I can change image if the images are one on the top of the other but I need something different.
I need to change the images when I mouse over another image. Like;
<div id="gallery">
<div>
<img src="images/team-large.jpg" alt="Img">
</div>
<ul>
<li>
<img src="images/elek2.jpg" alt="Img" title="Elektronik Alt Sistemler">
</li>
<li>
<img src="images/su.jpg" alt="Img" title="Sualtı Akustik Sistemler">
</li>
<li>
<img src="images/yazılım.jpg" alt="Img" title="Yazılım, Bilgi Teknolojileri ve Simülasyon">
</li>
</ul>
</div>
This is my HTML Code and I need to change
<img src="images/team-large.jpg" alt="Img">
this image when I mouse over the other sub images but I'm stuck.
Addition;
How can I change other images by onmouseover and onmouseout commands??
<div id="gallery">
<div>
<img src="images/team-large.jpg" id="Img1" name="Img1" class="Img1" alt="Img1" />
</div>
<ul>
<li>
<img src="images/elek2.jpg" alt="Img" title="Elektronik Alt Sistemler"
onmouseover="'#Img1'.src='images/elek3.jpg'" onmouseout="'#Img1'.src='images/team-large.jpg'">
</li>
<li>
<img src="images/su.jpg" alt="Img" title="Sualtı Akustik Sistemler" class="thumbnail">
</li>
<li>
<img src="images/yazılım.jpg" alt="Img" title="Yazılım, Bilgi Teknolojileri ve Simülasyon" class="thumbnail">
</li>
</ul>
</div>
I added the ID name to Img1 and tried to change the image when I mouse over 'images/elek2.jpg' but it doesn't work.
Thanks for help.
You could alter the background image using css's :hover instead. Something like:
div{
height:200px;
width:200px;
background:url("http://placekitten.com/g/200/200");
}
div:hover{
background:url("http://placekitten.com/g/200/400");
}
<div></div>
For what you're looking for, you might need the child or sibling selector:
.parent{
height:300px;
width:300px;
background: url("http://placekitten.com/g/300/300");
}
.child{
height:200px;
width:200px;
background: url("http://placekitten.com/g/200/200");
}
.parent:hover .child{
background: url("http://placekitten.com/g/200/300");
}
<div class="parent">
<div class="child"></div>
You may try this jQuery code to change source of main image
var original = $('#main').attr('src');
$('.thumbnail').mouseover(function()
{
var source = $(this).attr('src'); // retrieve image source of hovered image
$('#main').attr('src', source); // update main image source
})
.mouseout(function() {
$('#main').attr('src', original); // restore original image source
});
jsFiddle code (UPDATED)
In the linked snippet, I've assigned a main id to main image and a thumbnail class to other images. This allows to access them runtime via jQuery.