Страницы

Поиск по вопросам

среда, 1 января 2020 г.

Как я могу замаскировать перекрывающим элементом видимость текста, используя CSS?

#javascript #css #jquery #html5 #css_animation


На Gif ниже, текст появляется только после того, как через него проходит черная точка.
До этого текст невидим. 
 Это было сделано с помощью Flash, но как мы можем добиться этого с помощью CSS?    



Вот что у меня получилось, но это  далеко от желаемого результата: 

Я думал сделать div и зафиксировать его правую границу с центральной осью точки посередине,
но это также скрывает зеленый и красный элементы.    



@import url("https://fonts.googleapis.com/css?family=Ubuntu+Mono");
body {
  height: 100vh;
  background: #222;
  padding: 0;
  margin: 0;
  font-family: "Ubuntu Mono";
}

.ypn-logo {
  background: green;
  display: flex;
  flex-direction: row;
  position: relative;
  align-items: center;
  justify-content: center;
  padding: 10px;
  width: 220px;
  height: 220px;
  border-radius: 220px;
  z-index: 1;
}

.ypn-text {
  font-size: 3.5em;
  color: white;
  transition: all 1s cubic-bezier(0.86, 0, 0.07, 1);
  margin-left: -80px;
  z-index: 0;
}

.ypn-before {
  background: red;
  content: "";
  position: absolute;
  width: 180px;
  height: 180px;
  border-radius: 180px;
  z-index: -1;
  transition: all 1s cubic-bezier(0.86, 0, 0.07, 1);
  top: 30px;
  left: 10px;
}

.ypn-logo:hover>.ypn-before {
  left: 50px;
}

.ypn-after {
  background: #222;
  content: "";
  position: absolute;
  width: 50px;
  height: 50px;
  border-radius: 180px;
  transition: all 1s cubic-bezier(0.86, 0, 0.07, 1);
  top: 95px;
  left: 130px;
  z-index: 1;
}

.ypn-logo:hover>.ypn-after {
  left: 60px;
}

.ypn-logo:hover>.ypn-text {
  margin-left: 80px;
}

.ypn-after:after {
  width: 130px;
  background: black;
  height: 3em;
  content: '';
  position: absolute;
  left: -100px;
}



  

Есть ли способ просто заблокировать текстовый элемент, но не другие элементы? 
    


Ответы

Ответ 1



Пример, где используются псевдоэлементы, after для круга и before для перекрытия текста и дополнительная обертка для текста. для круга - position: absolute; right: 0, тем самым прижимаем его изначально к правому краю для перекрытия задаем фон в цвет блока и растягиваем его на всю ширину и высоту блока, но right указываем не 0, а половину ширины круга для того, чтобы при движении текст не перекрывался белой частью прекрытия, выступающей за круг для обертки текста задаем position: relative, чтобы работало св-во right и сам изначальный right, делается именно так, а не просто margin-right для того, чтобы ширина блока оставалась одинаковой ну и при ховере всё это анимируем, круг и перекрытие смещаем влево, а тест в право, для текста я указал время transition больше, чем для круга, чтобы анимаци была максимально похожа на гифку. Таким образом можно изменять текст внутри лого и изменять отступ круга от текста и всё будет работать одинаково. .logo{ display: inline-block; font-size: 35px; font-weight: bold; font-family: Tahoma; padding-left: 44px; border: 1px solid #ececec; position: relative; overflow: hidden; } .logo-text { position: relative; right: 100px; transition: 600ms; } .logo:after, .logo:before{ content: ''; position: absolute; top: 0; height: 100%; transition: 400ms; z-index:1; } .logo:after{ width: 42px; background: #000; right: 0; border-radius: 50%; } .logo:before{ width: 100%; right: 21px; background: #fff; } .logo:hover:before{ right: calc(100% - 21px); } .logo:hover:after{ right: calc(100% - 42px); } .logo:hover .logo-text{ right: 0; }



Ответ 2



Вы можете сделать текст дочерним для элемента с цветом фона. Затем создайте маску, создав элемент с точкой и элементом div, установленным на цвет фона родительского элемента. Сделайте overflow:hidden родительского объекта: скрытым, чтобы цветная область не была видна при перемещении, чтобы раскрыть текст. $('.overlay').on('click', function() { $(this).toggleClass('hidden'); }); $(window).on('load', function() { $('.overlay').removeClass('hidden'); }); body { background: red; } .container { width: 80%; height: 60px; border-radius: 60px; background: white; overflow: hidden; margin: 20px auto; position: relative; } .overlay { width: 100%; height: 100%; background: white; transition: left 2s ease-out; position: absolute; top: 0; left: calc( -100% + 60px); } .overlay.hidden { left: 0; } .overlay::after { content: ''; background: black; height: 60px; width: 60px; border-radius: 60px; position: absolute; right: 0; top: 0; } .text { font-size: 50px; line-height: 60px; width: 100%; text-align: center; } p { text-align: center; }
ezoom

Щелкните по кругу, чтобы переключить анимацию

После использования вышеуказанного принципа, вот окончательный код нужного вам эффекта: @import url("https://fonts.googleapis.com/css?family=Ubuntu+Mono"); body { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; padding: 0; margin: 0; background: #222; font-family: "Ubuntu Mono"; } .ypn-logo { background: green; display: flex; flex-direction: row; position: relative; align-items: center; justify-content: center; padding: 10px; width: 220px; height: 220px; border-radius: 220px; } .ypn-before { background: red; content: ''; overflow: hidden; width: 180px; height: 180px; border-radius: 180px; display: flex; flex-direction: row; align-items: center; justify-content: center; position: relative; left: -20px; transition: all 1s cubic-bezier(0.86, 0, 0.07, 1); } .ypn-text { background: none; position: absolute; left: 20px; font-size: 3.2em; color: #ddd; transition: all 1s cubic-bezier(0.86, 0, 0.07, 1); } .ypn-dot { width: 200px; height: 200px; position: relative; background: red; left: -35px; transition: all 1s cubic-bezier(0.86, 0, 0.07, 1); } .ypn-dot:after { content: ''; position: absolute; background: #222; width: 60px; height: 60px; border-radius: 60px; top: 70px; right: -25px; transition: all 1s cubic-bezier(0.86, 0, 0.07, 1); } .ypn-logo:hover>.ypn-before { left: 20px; } .ypn-logo:hover>.ypn-before .ypn-dot { left: -135px; } .ypn-logo:hover>.ypn-before .ypn-text { left: 80px; }

Ответ 3



Вот идея, где вы можете использовать один элемент и рассмотреть анимацию width/margin: .box { font-size: 50px; font-weight: bold; line-height: 1em; display: inline-flex; justify-content:flex-end; overflow:hidden; white-space:nowrap; border-radius:1em 0 0 1em; background: radial-gradient(#000 0.48em, transparent 0.5em) left/1em 1em no-repeat; width:0; margin-left:200px; padding: 5px 1em 5px 0; transition:1s; } body:hover .box { width:200px; margin-left:0px; padding: 5px 0 5px 1em; }
some text
Чтобы избежать установки определенной ширины, вы можете настроить выравнивание и рассмотреть максимальную ширину max-width .container { text-align:right; width:500px; } .box { font-size: 50px; font-weight: bold; line-height: 1em; display: inline-flex; justify-content:flex-end; overflow:hidden; white-space:nowrap; border-radius:1em 0 0 1em; background: radial-gradient(#000 0.48em, transparent 0.5em) left/1em 1em no-repeat; max-width:0; padding: 5px 1em 5px 0; transition:max-width 1s,padding 0s 1s; } body:hover .box { max-width:100%; padding: 5px 0 5px 1em; transition:max-width 1s; }
some text


Комментариев нет:

Отправить комментарий