原生JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Input() src: string; //图片资源地址
@Input() isShowTip = true; //是否展示
@Input() preSize = 100; // 缩略图尺寸
@Input() showSize = 700; //展示尺寸

//@Input是封装成angular组件了,纯js 可以当成 let src = '';

// 绑定在悬浮事件 onmouseover
showPreview($event){
let pageHeight = $event.pageY; // 图片在body中的高度位置
let pageWidth = $event.pageX; // 图片在body中的宽度位置
let preTip = document.createElement('div');
preTip.setAttribute('id', 'preTip');
preTip.style.zIndex = '99';
preTip.style.bottom = window.document.body.scrollHeight - pageHeight - 250 + 'px';
preTip.style.left = pageWidth > window.document.body.clientWidth/2 ? pageWidth - this.showSize - 50 + 'px' : pageWidth + 50 + 'px';
preTip.style.position = 'absolute';
let preImg = document.createElement('img');
preImg.setAttribute('src', this._src);
preImg.style.width = '700px';
preImg.style.height = '700px';
preTip.appendChild(preImg)
window.document.querySelector('body').appendChild(preTip); //把它追加到文档中
}

// onmouseout 同理
removePreview() {
window.document.body.removeChild(window.document.querySelector('#preTip'));
}

考虑到复用就封装了简易的angular组件

TS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Component, OnInit, Input } from '@angular/core';

@Component({
selector: 'app-mypreview',
templateUrl: './mypreview.component.html',
styleUrls: ['./mypreview.component.css']
})
export class MypreviewComponent implements OnInit {

@Input() src: string; //图片资源地址
@Input() isShowTip = true; //是否展示
@Input() preSize = 100; // 缩略图尺寸
@Input() showSize = 700; //展示尺寸

constructor() { }

ngOnInit() {
this.changeSize();
}

private _src = '';
private changeSize() {
this._src = this.src.replace(`/${this.preSize}x${this.preSize}/`, `/${this.showSize}x${this.showSize}/`);
}

showPreview($event): boolean {
if (this.isShowTip === false) {
return false;
}
let pageHeight = $event.pageY; // 图片在body中的高度位置
let pageWidth = $event.pageX; // 图片在body中的宽度位置
let preTip = document.createElement('div');
preTip.setAttribute('id', 'preTip');
preTip.style.zIndex = '99';
preTip.style.bottom = window.document.body.scrollHeight - pageHeight - 250 + 'px';
preTip.style.left = pageWidth > window.document.body.clientWidth/2 ? pageWidth - this.showSize - 50 + 'px' : pageWidth + 50 + 'px';
preTip.style.position = 'absolute';
let preImg = document.createElement('img');
preImg.setAttribute('src', this._src);
preImg.style.width = '700px';
preImg.style.height = '700px';
preTip.appendChild(preImg)
window.document.querySelector('body').appendChild(preTip); //把它追加到文档中
return true;
}
removePreview() {
this.isShowTip !== false && window.document.body.removeChild(window.document.querySelector('#preTip'));
}

}

HTML

1
<img [src]="src" alt="" (mouseover)=showPreview($event) (mouseout)="removePreview()" appErrSrc>

父组件module引入

1
2
3
4
5
6
7
import { MypreviewComponent } from '../../../../../component/mypreview/mypreview.component'

@NgModule({
declarations: [
MypreviewComponent
]
})

父组件HTML

1
<app-mypreview [isShowTip]="true" [preSize]="100" [src]="xxx"></app-mypreview>