【问题标题】:Wrap dynamic text around a spinning cylinder with CSS/Javascript (splitting.js)使用 CSS/Javascript (splitting.js) 将动态文本包裹在旋转的圆柱体周围
【发布时间】:2021-03-22 07:18:13
【问题描述】:

我对编码还很陌生,并且没有深入的知识来独自解决这个问题。如果有人可以帮助我,我将非常感激!

我正在尝试使用 CSS 和 split.js 将动态加载的文本包装在旋转圆柱体周围,而没有固定数量的字符。我尝试关注this tutorial,一切都很好,直到我开始更改文本。问题是这种方法只适用于长度不变的文本,因为如果文本太长它会被截断,或者如果太短会导致圆柱体中的间隙。

Here 是我现在拥有的源代码。可悲的是,当我将其粘贴到 jsfiddle 中时,它无法正常工作。然而,它在我的代码编辑器中工作得很好,并且与我上面链接的教程相同。

        <div class="circle" data-splitting>
        Circle-Text-Animation-Effect-Cool-great
    </div>
    <script>
        Splitting();
    </script>


    * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: monospace;
}

body{
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color:aqua;
}


.circle {
    transform-style: preserve-3d;
    animation: animate 8s linear infinite;
}

@keyframes animate {
    0% {
        transform: perspective(1000px) rotateY(360deg) rotateX(15deg);
    }

    100% {
        transform: perspective(1000px) rotateY(0deg) rotateX(15deg);
    }
}

.circle .char {
    position: absolute;
    top: 0;
    left: 0;
    background: red;
    color: blue;
    font-size: 4em;
    padding: 5px 12px;
    border: 4px solid black;
    transform-style: preserve-3d;
    transform-origin: center;
        transform: rotateY(calc(var(--char-index) * 10deg)) translateZ(250px);

}

这个问题有什么解决方法吗?甚至可以不使用 split.js?

我希望我能正确描述我的问题。英语不是我的母语,我还不能将图片上传到 Stackoverflow,所以我无法直观地描述问题!

提前感谢您的帮助!

enter image description here

【问题讨论】:

    标签: javascript html jquery css css-animations


    【解决方案1】:

    这里的技巧是要注意,你需要旋转一个字符的度数取决于字符串中的字符总数。

    我没有 splitter.js 库,所以放了一些 JS 来做同样的事情 - 用定义 CSS 变量的样式将每个字符分隔到自己的 div 中 - 字符的索引。

    JS 还设置了一个新的 CSS 变量 --numchs,在 CSS 中用于计算每个字符旋转的度数 --deg。然后使用它而不是 10 度来决定放置角色的位置。

            const circle = document.querySelector('.circle');
            const text = circle.innerHTML;// Note I am being lazy here and assuming the string has no unwanted whitespace
            circle.innerHTML = '';        
            circle.style.setProperty('--numchs', text.length);
            for ( let i = 0; i < text.length; i++ ) {
              circle.innerHTML = circle.innerHTML + '<div class="char" style="--char-index: ' + i + ';">' + text.charAt(i) + '</div>';
            }
        * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: monospace;
    }
    
    body{
        display: flex;
        justify-content: center;
        align-items: center;
        min-height: 100vh;
        background-color:aqua;
    }
    
    
    .circle {
        transform-style: preserve-3d;
        animation: animate 8s linear infinite;
        --deg: calc(360deg / var(--numchs));
    }
    
    @keyframes animate {
        0% {
            transform: perspective(1000px) rotateY(360deg) rotateX(15deg);
        }
    
        100% {
            transform: perspective(1000px) rotateY(0deg) rotateX(15deg);
        }
    }
    
    .circle .char {
        position: absolute;
        top: 0;
        left: 0;
        background: red;
        color: blue;
        font-size: 4em;
        padding: 5px 12px;
        border: 4px solid black;
        transform-style: preserve-3d;
        transform-origin: center;
            transform: rotateY(calc(var(--char-index) * var(--deg))) translateZ(250px);
    
    }
    &lt;div class="circle" data-splitting&gt;Circle-Text-Animation-Effect-Cool-great&lt;/div&gt;

    【讨论】:

      猜你喜欢
      • 2014-12-16
      • 2011-08-23
      • 2017-04-21
      • 1970-01-01
      • 2020-08-17
      • 2020-06-28
      • 2021-11-04
      • 2019-08-05
      • 2011-10-01
      相关资源
      最近更新 更多