Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: img新增对srcset属性的支持 #73

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion components/xy-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ if (!customElements.get('xy-gallery')) {

export default class XyImg extends HTMLElement {

static get observedAttributes() { return ['lazy', 'src', 'defaultsrc', 'ratio'] }
static get observedAttributes() { return ['lazy', 'src', 'defaultsrc', 'ratio', 'srcset'] }

constructor() {
super();
Expand Down Expand Up @@ -517,6 +517,10 @@ export default class XyImg extends HTMLElement {
return this.getAttribute('src');
}

get srcset() {
return this.getAttribute('srcset')
}

get defaultsrc() {
return this.getAttribute('defaultsrc');
}
Expand Down Expand Up @@ -559,6 +563,10 @@ export default class XyImg extends HTMLElement {
this.setAttribute('src', value);
}

set srcset(value) {
this.setAttribute('srcset', value)
}

set fit(value) {
this.setAttribute('fit', value);
}
Expand Down Expand Up @@ -593,6 +601,9 @@ export default class XyImg extends HTMLElement {
img.onload = () => {
this.img.alt = this.alt;
this.img.src = src;
if (this.srcset) {
this.img.srcset = this.srcset;
}
if(!this.default){
this.initgallery();
}
Expand Down
7 changes: 7 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,13 @@ <h2>img</h2>
<xy-img lazy gallery src="https://images.pexels.com/photos/698808/pexels-photo-698808.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"></xy-img>
<xy-img lazy gallery src="https://images.pexels.com/photos/1440387/pexels-photo-1440387.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"></xy-img>
<xy-img lazy gallery alt="image-keyboard" src="https://images.pexels.com/xxx.jpg" defaultsrc="https://images.pexels.com/photos/697662/pexels-photo-697662.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"></xy-img>
<p>srcset</p>
<xy-img
src="https://s1.ax1x.com/2023/01/22/pSJu3ND.jpg"
srcset="
https://s1.ax1x.com/2023/01/22/pSJu84e.png 2x,
https://s1.ax1x.com/2023/01/22/pSJuJ9H.png 3x"
></xy-img>
<h2>rate</h2>
<xy-rate onchange="console.log(event)"></xy-rate>
<p>tips</p>
Expand Down