gpt4 book ai didi

javascript - 鼠标悬停在列表上时传单打开弹出窗口

转载 作者:行者123 更新时间:2023-11-30 19:51:19 24 4
gpt4 key购买 nike

我的应用程序的一个 View 是,一侧是所有引脚的列表,另一侧是带有实际引脚的 map 。

我希望在我用鼠标悬停在列表中时打开弹出窗口。假设我的鼠标在列表中的第一位,我希望打开相应 pin 的弹出窗口。

这是来自列表组件的 html:

    <table class="table-striped">
<thead>
<!-- <th>id</th> -->
<th>Kunstwerkname</th>
<th>Strasse</th>
<th>PLZ</th>
<th>
<button class="button-list" (click)="addArtwork()">
<img src="assets/icons/add.svg">
</button>
</th>
</thead>
<tbody>
<tr *ngFor="let artwork of artworkList;" class="artworkList" (mouseover)="previewPopup(artwork)">
<td>{{artwork.name}}</td>
<td>{{artwork.streetname}}</td>
<td>{{artwork.zipcode}}</td>
<td>
<button class="button-list" (click)="editArtwork(artwork)">
<img src="assets/icons/edit.svg">
</button>
<button class="button-list" (click)="deleteArtwork(artwork)">
<img src="assets/icons/delete.svg">
</button>
</td>
</tr>
</tbody>
</table>

列表组件的ts:

import { Artwork, ArtworkService } from './../_services/artwork.service';
import { Component, OnInit, Output, EventEmitter } from '@angular/core';


@Component({
selector: 'app-artwork-list',
templateUrl: './artwork-list.component.html',
styleUrls: ['./artwork-list.component.css']
})
export class ArtworkListComponent implements OnInit {
@Output() private add = new EventEmitter();
@Output() private edit = new EventEmitter<number>();
@Output() private preview = new EventEmitter<number>();
artworkList: Artwork[];

constructor(
private artworkService: ArtworkService,
) { }

ngOnInit() {
this.refresh();
}
refresh() {
this.artworkService.retrieveAll().then(
artworkList => this.artworkList = artworkList
);
}

addArtwork() {
console.log('add artwork');
this.add.emit();
}

editArtwork(artwork: Artwork) {
console.log('edit artwork ' + artwork.name + ' ' + artwork.id );
this.edit.emit(artwork.id);
}

deleteArtwork(artwork: Artwork) {
console.log('delete artwork ' + artwork.name + ' ' + artwork.id );
this.artworkService.delete(artwork.id).then(
() => this.refresh()
);
}

previewPopup(artwork: Artwork) {
console.log("hovering mouse");
console.log(artwork.id);
this.preview.emit(artwork.id);
}

}

和 map 组件的ts:

import { Component, OnInit, Output, EventEmitter, ElementRef, Input } from '@angular/core';

/*When you include the leaflet script inside the Angular project, it gets
loaded and exported into a L variable.*/
declare let L; //this is the leaflet variable!

import { MapButtonsComponent } from './map-buttons/map-buttons';
import { Artwork, ArtworkService } from '../_services/artwork.service';
import { FilterMapComponent } from './filter-map/filter-map.component';



@Component({
selector: 'app-open-street-map',
templateUrl: './open-street-map.component.html',
styleUrls: ['./open-street-map.component.css']
})
export class OpenStreetMapComponent implements OnInit {
@Output() private add = new EventEmitter();
@Output() private edit = new EventEmitter<number>();
artworkList: Artwork[];
map;

// markerIcon
markerIcon = {
icon: L.icon({
iconSize: [25, 41],
iconAnchor: [13, 16],
iconUrl: 'assets/icons/marker.svg',
shadowUrl: 'assets/icons/marker-shadow.png'
})
};

constructor(
private artworkService: ArtworkService,
private elementRef: ElementRef
) { }

ngOnInit() {
this.map = L.map('map', {
center: [48.208, 16.373],
zoom: 13,
zoomControl: false,
});

this.refresh();

// base layer
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
minZoom: 13,
id: 'mapbox.streets',
accessToken: 'pk.eyJ1IjoicHcxN2wwMDgiLCJhIjoiY2pua2c2OWxuMGVkOTNxbWh5MWNqajEwdyJ9.X_SuGwNGs12TwCsrsUvBxw'
}).addTo(this.map);

MapButtonsComponent.renderZoom(this.map);
MapButtonsComponent.renderCompass(this.map);
MapButtonsComponent.renderLocation(this.map);
}

previewArtwork() {
this.map.eachLayer(layer => {
if (layer instanceof L.Marker) {
this.map.removeLayer(layer);
}
});
let previewedMarker = L.marker([artwork.latitude, artwork.longitude], this.markerIcon)
.addTo(this.map)
.on('mouseover', () => {
openPopup();
});
}

}
}

最佳答案

可以考虑以下方法。让我们假设标记的初始数据以以下格式表示:

locations = [
{
name: "Oslo",
lat: 59.923043,
lng: 10.752839
},
{
name: "Stockholm",
lat: 59.339025,
lng: 18.065818
},
//..
];

然后对于给定的数据, map 上的标记可以这样初始化:

this.markers = this.locations.map(loc => {
const marker = new L.marker({ lat: loc.lat, lng: loc.lng });
marker.bindPopup(loc.name);
marker.addTo(map);
return marker;
});

现在轮到选择器组件和 map 本身之间的通信了,让我们假设以下选择器:

<ul>
<li (mouseenter) ="mouseEnter(idx) " (mouseleave) ="mouseLeave(idx)" *ngFor="let loc of locations; let idx = index">{{ loc.name }}</li>
</ul>

然后可以像这样控制弹出窗口的可见性:

mouseEnter(selectedIndex) {
const selectedMarker = this.markers[selectedIndex];
selectedMarker.openPopup();
}

mouseLeave(selectedIndex) {
const selectedMarker = this.markers[selectedIndex];
selectedMarker.closePopup();
}

Here is a demo

关于javascript - 鼠标悬停在列表上时传单打开弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54428857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com