CSS代码:
zxx-circle {
display: block;
width: 207px; height: 250px;
color: white;
background-color: deepskyblue;
text-shadow: 1px 1px rgba(0,0,0,.5);
padding: 10px;
text-align: justify;
}
HTML代码:
<zxx-circle>在CSS Shapes布局问世之前,文字像杂志一样的任意形状的排版几乎是不可能的,一直都是用网格、盒子和直线构造。CSS Shapes允许我们定义文本环绕的几何形状。这些形状可以是圆、椭圆、简单或复杂的多边形,甚至是任意的图像和多彩的渐变。</zxx-circle>
JS代码:
class HTMLZxxCircleElement extends HTMLElement {
constructor() {
self = super();
let shadow = this.attachShadow({
mode: 'open'
});
let style = document.createElement('style');
shadow.appendChild(style);
let before = document.createElement('zxx-before');
let after = document.createElement('zxx-after');
shadow.prepend(after);
shadow.prepend(before);
let content = document.createElement('div');
shadow.appendChild(content);
let ro = new ResizeObserver( entries => {
for (let entry of entries) {
self._updateRendering();
}
});
ro.observe(this);
}
connectedCallback() {
this._updateRendering();
}
_updateRendering() {
let shadow = this.shadowRoot;
let content = shadow.querySelector('div');
let before = shadow.querySelector('zxx-before');
before.style.height = 'auto';
content.innerHTML = this.innerHTML;
let heightContent = parseFloat(getComputedStyle(content).height);
let heightBefore = parseFloat(getComputedStyle(before).height);
if (heightContent == 0) {
return;
}
if (heightBefore == 0 && heightContent != 0) {
let height = (2 * heightContent * 2 / Math.PI) + 'px';
console.warn('<zxx-circle> no height, set it to ' + height + '!');
this.style.height = height;
}
before.removeAttribute('style');
shadow.querySelector('style').textContent = `:host {
border-radius: 50%;
}
zxx-before,
zxx-after {
width: 50%; min-height: 100%; height: ${heightContent}px;
}
zxx-before {
float: left;
shape-outside: radial-gradient(farthest-side ellipse at right, transparent 100%, red);
}
zxx-after {
float: right;
shape-outside: radial-gradient(farthest-side ellipse at left, transparent 100%, red);
}`;
}
}
customElements.define('zxx-circle', HTMLZxxCircleElement);