From the screenshot provided, it is clear that there is a background image for the page and two thumbnails positioned below the text. The objective is to dynamically change the background image of the page to match the thumbnail image when the mouse hovers over the thumbnail.
The code snippet below shows a directive that attempts to achieve this functionality. The main challenge faced is injecting the thumbnail image source into the element to update the page background. Additionally, an error message indicating "Expected 2 arguments, but got 1" is encountered while using the ViewChild method.
import { Directive, ElementRef, HostListener, ViewChild } from '@angular/core';
@Directive({
selector: '[dynamicBackgroundImg]'
})
export class DynamicBackgroundImgDirective {
thumbSRC : string;
@ViewChild('tourBackgroundImg') tourBackgroundImg:ElementRef;
constructor(private el: ElementRef) {}
@HostListener('mouseover') onMouseOver() {
this.ChangeBackgroundImg();
}
@HostListener('mouseleave') onMouseLeave() {
}
ChangeBackgroundImg() {
this.thumbSRC = this.el.nativeElement.getAttribute('src');
alert(this.thumbSRC);
this.tourBackgroundImg.nativeElement.setAttribute(this.thumbImgSrc);
}
}
Here is a condensed version of the corresponding HTML code:
<section class="tours-section" [ngClass]="{ 'load-complete' : viewPage }">
<img class="component-loader" src="../../../assets/icons/loading.svg">
<div class="tours-wrapper">
<div class="tours-right page-title">
<p>{{pageTitle}}</p>
<picture>
<source srcset="{{ toursImageUrl }}?fm=webp" type="image/webp">
<source srcset="{{ toursImageUrl }}?fm=png" type="image/png">
<!-- The following tag controls the background image of the page, and should be dynamically updated based on the thumbnail image being hovered over. -->
<img src="{{ toursImageUrl }}?fm=png" alt="{{ toursImageAlt }}" (load)="imageLoaded()" class="section-background" tourBackgroundImg>
</picture>
</div>
<div class="tours-left">
<div class="tours-thumbs">
<div class="tours-container">
<!-- When a user hovers over a thumbnail, the goal is to extract and use the src of the tour image -->
<figure class="tour-image-small">
<picture>
<img src="assets/images/L1000433-min.JPG" alt="" dynamicBackgroundImg>
<figcaption></figcaption>
</picture>
</figure>
</div>
</div>
</div>
</div>
</div>
</section>
Here is a link to a screenshot for better visualization of the desired outcome:
https://i.sstatic.net/T2FmI.jpg
Any insights or suggestions on a different approach to achieving this functionality would be greatly appreciated.