ResizeObserver实现textarea边框颜色变啊变实例页面

回到相关文章 »

效果:

代码:

CSS代码:
textarea {
    width: 200px; height: 100px;
    border-image: linear-gradient(deepskyblue, deeppink) 1;    
}
HTML代码:
<textarea id="roElement">本文地址:https://www.zhangxinxu.com/wordpress/?p=9295
作者:zhangxinxu</textarea>
                
JS代码:
var eleRo = document.getElementById('roElement');
var objResizeObserver = new ResizeObserver(function (entries) {
    var entry = entries[0];
    var cr = entry.contentRect;
    var target = entry.target;
    
    var angle = (cr.width - 200) + (cr.height - 100);
    target.style.borderImageSource = 'linear-gradient('+ angle +'deg, deepskyblue, deeppink)';
});
// 观察文本域元素
objResizeObserver.observe(eleRo);