Smoothly changing text with JavaScript

前端 未结 4 1062
故里飘歌
故里飘歌 2021-02-01 03:54

I\'m making a landing page where a phrase is constantly changing with select words. For instance,

Design better websites
made for clients.

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 04:44

    Got some good answers.
    Alin Purcaru has a much better and more coherently-written answer, but I thought I'd provide my own.

    Nit had what I was looking for, but since I'm not the best programmer, I tried to come up with a solution I could understand. After an hour or two, here's what I got.

    Basically, I'm comparing the full block of text to the parent element, finding the space between them, halving it, and then applying that as a negative margin to the text. I can transition this with CSS since I'm moving a full block.

    Here's a very bad drawing in MSpaint to illustrate my point

    enter image description here

    the text has display: inline-block so the div fits to the text rather than taking up 100% of the parent.

    Since I was transition with CSS in my javascript, all I needed to do to make it smooth was

      -webkit-transition: all 1s ease-in-out;
      -moz-transition: all 1s ease-in-out;
      -o-transition: all 1s ease-in-out;
      transition: all 1s ease-in-out;
    

提交回复
热议问题