feat(ipub): make sticky and fade backgrounds the default
This commit is contained in:
@@ -38,9 +38,7 @@ class IPUBElement extends HTMLElement {
|
||||
|
||||
class IPUBBackground extends IPUBElement {
|
||||
static elementName = "ipub-background";
|
||||
static observedAttributes = ["sticky", "fade"].concat(
|
||||
super.observedAttributes,
|
||||
);
|
||||
static observedAttributes = ["nofade"].concat(super.observedAttributes);
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -89,25 +87,51 @@ class IPUBBackground extends IPUBElement {
|
||||
);
|
||||
})();
|
||||
|
||||
connectedCallback() {
|
||||
super.connectedCallback();
|
||||
|
||||
const image = this.querySelector("img");
|
||||
if (!image) {
|
||||
console.error("IPUBBackground: missing <img> element inside background");
|
||||
return;
|
||||
}
|
||||
|
||||
// INFO: We don't need to fade the first background
|
||||
if (this.matches(":first-of-type") || this.hasAttribute("nofade")) {
|
||||
IPUBBackground.#observer.unobserve(image);
|
||||
return;
|
||||
}
|
||||
|
||||
IPUBBackground.#observer.observe(image);
|
||||
const perc = getPercentageInView(image);
|
||||
if (perc > 0) {
|
||||
this.fade(perc);
|
||||
}
|
||||
}
|
||||
|
||||
attributeChangedCallback(name, _oldValue, _newValue) {
|
||||
super.attributeChangedCallback();
|
||||
|
||||
if (name === "fade") {
|
||||
const image = this.querySelector("img");
|
||||
if (image) {
|
||||
console.debug(`IPUBBackground: ipub-background#${this.id} to observer`);
|
||||
if (name !== "nofade") {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.hasAttribute("fade")) {
|
||||
IPUBBackground.#observer.observe(image);
|
||||
} else {
|
||||
IPUBBackground.#observer.unobserve(image);
|
||||
}
|
||||
const image = this.querySelector("img");
|
||||
if (!image) {
|
||||
console.error("IPUBBackground: missing <img> element inside background");
|
||||
return;
|
||||
}
|
||||
|
||||
const perc = getPercentageInView(image);
|
||||
if (perc > 0) {
|
||||
this.fade(perc);
|
||||
}
|
||||
}
|
||||
// INFO: We don't need to fade the first background
|
||||
if (this.matches(":first-of-type") || this.hasAttribute("nofade")) {
|
||||
IPUBBackground.#observer.unobserve(image);
|
||||
return;
|
||||
}
|
||||
|
||||
IPUBBackground.#observer.observe(image);
|
||||
const perc = getPercentageInView(image);
|
||||
if (perc > 0) {
|
||||
this.fade(perc);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
<body xmlns:epub="http://www.idpf.org/2007/ops" class="body">
|
||||
<ipub-content style="--ipub-padding: 10%;">
|
||||
<main>
|
||||
<ipub-background id="background0001" sticky="">
|
||||
<ipub-background id="background0001">
|
||||
<img src="../images/background0001.jpg" width="100" height="100" />
|
||||
</ipub-background>
|
||||
<ipub-image>
|
||||
@@ -43,7 +43,7 @@
|
||||
<source src="../audios/audio0001.wav.disable" />
|
||||
</audio>
|
||||
</section>
|
||||
<ipub-background sticky="" fade="" id="background0002">
|
||||
<ipub-background id="background0002">
|
||||
<picture>
|
||||
<img src="../images/background0002.jpg" />
|
||||
</picture>
|
||||
@@ -58,7 +58,7 @@
|
||||
<img src="../images/image0004.png" />
|
||||
</span>
|
||||
</section>
|
||||
<ipub-background sticky="" fade="" id="background0003">
|
||||
<ipub-background id="background0003">
|
||||
<picture>
|
||||
<img src="../images/background0003.jpg" />
|
||||
</picture>
|
||||
|
||||
Reference in New Issue
Block a user