VueJS - apply a rotation animation before showing the element

I'm having trouble displaying a line in different directions. While vertical and horizontal lines work fine, the diagonal direction requires rotation before it appears correctly. The issue is that it initially displays vertically before rotating to 135 degrees. I understand that the element needs to be displayed first before being rotated. How can I resolve this problem?

Check out a working demo

HTML:

<div id="app">
  <button @click="toggle">Toggle</button>
  <span>Direction: {{ direction }}</span>
  <transition name="line-transition">
    <div v-if="direction"
       class="line"
      :class="direction">
    </div>
  </transition>
</div>

VueJS

new Vue({
  el: "#app",
  data: {
    direction: null
  },
  methods: {
    toggle: function(){
      const arr = [ 'vertical', 'horizontal', 'diagonal']

      /* randomly select */
      this.direction = arr[Math.floor(Math.random() * arr.length)]

      /* hide line before toggling direction */
      setTimeout(() => {
            this.direction = null
      }, 1000)
    }
  }
})

CSS:

.line.vertical {
  width: 10px;
  height: 100px;
}
.line.horizontal {
  width: 100px;
  height: 10px;
}
.line.diagonal {
  transform: rotate(135deg);
  width: 10px;
  height: 100px;
}

.line-transition-enter-active,
.line-transition-leave-active {
  animation: slidein 1s;
}

@keyframes slidein {
  from {
    transform: scaleX(0) scaleY(0);
  }
   to {
    transform: scaleX(1) scaleY(1);
  }
}

Answer №1

An innovative approach involves using a pseudo element to generate the diagonal line and prevent conflicts with applying transform to the animated element:

.line.diagonal {
  width:100px;
  height:100px;
  background:transparent;
}
.line.diagonal:before {
  content:"";
  top:0;
  left: calc(50% - 5px);
  position:absolute;
  transform: rotate(135deg);
  border-radius: inherit;
  background: blue;
  width: 10px;
  height: 100px;
}

Here is the complete code snippet:

https://jsfiddle.net/m4g198r2/28/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

excess margin running along the left side of each page

When visiting http://tadris3.ir/, one may notice a peculiar space on the left side of all pages. Can anyone assist in resolving this bothersome issue? https://i.sstatic.net/MsX1a.png ...

I am having difficulty aligning text horizontally within a <div> element using a combination of <div>, <a>, and <p> tags

I can't seem to figure out why I'm struggling with this. Check out the screenshot below: Here is my current CSS: #tabbase { display : inline-block; width : 230px; height : 173px; border : 1px solid #3e3e3e; background-color : #1b ...

tcpdf - bug causing incorrect indentation following a line break

Are you familiar with how to adjust text alignment after a line break using tcpdf? With tcpdf 6.2.13 and the writeHTML method, I am attempting to achieve perfectly left-aligned text. However, whenever the line is too long, the following line gets shifted ...

Bootstrap Collapse Plugin allows you to toggle the visibility of

Is there a way for all the multicollapses to automatically close when a new one is opened? I specifically want the "Accordion Item # 2.1" to collapse when I click on "Accordion Item # 1" or "Accordion Item # 3". Currently, my code only closes "Accordion It ...

Why isn't the HTML template opening in Outlook 7?

Does anyone know how to fix the code issue on Outlook 7? I've only included the header section of my code here, but please let me know if you need more information. I would appreciate any help with this problem. Thanks! ...

Setting the height of a div tag in CSS/HTML5 to match the current height of the browser window

Is there a way to make the height of my div tag always equal to the height of the browser's window, so it adjusts automatically when the browser is resized? I have tried using .class { height: 100%; } But this doesn't work. I've learne ...

Guide on aligning all list items to the left using React Material-UI, incorporating fontAwesome icons, and styling with Tailwind.css

I am trying to align the text of my list items to the left. Here is what I have currently: https://i.stack.imgur.com/a5o5e.png Is there a way to left-align the text in this code snippet? import React from 'react'; import PropTypes from 'p ...

Quirky rendering problem in AngularJS

My issue involves creating blocks using data from a JSON file, which includes a background-image path for a div. Here is an example snippet: [ .. { .. .. "smallimg": "../assets/images/dummy/dummy-intro-1.jpg", .. ] On my page, I hav ...

Guide to positioning a link on the right side of a navigation bar

I am trying to achieve a specific layout using Bootstrap. I want to align the logout button to the right-hand side and have three modules (contact us, about epm, and modules) centered on the page. Can someone guide me on how to achieve this using Bootstr ...

Verify the entered information in the input field and showcase it in the display box after pressing the ENTER key

I need to display selected values of a tree structure in a show box on the other side of the page using code. However, I also need to include HTML input boxes in some lines of the tree structure so that users can input data. The challenge is getting the in ...

Ensure that text is justified to the left both above and below inline elements contained within the parent div

I am attempting to align text, generated dynamically, on the left side of some images that are positioned next to each other. The image on the far left should also be aligned to the left. .inline-attachments { display:inline; vertical-align:top; } ...

Troubles with NextJS and TailwindCSS Styling

I encountered a strange issue when I used the component separately. Here's how the code looked like: <> <Head> <title>Staycation | Home</title> <meta name="viewport" content="initial- ...

Ensure the HTML table's <td> cell width automatically adjusts to accommodate the image, regardless of its dimensions

Has anyone encountered a strange issue while building an HTML table for an HTML email? Whenever I insert an image into a <td>, it causes the cell to expand to its maximum width, affecting all the columns to the right by resizing them to their minimum ...

"Using CSS to align content in a table-row format on an HTML display

I'm struggling to align two components using the <div display='table-cell'> method. The first component is showing a gap at the top which I want to remove in order to have them aligned perfectly. Check out the JsFiddle example here: h ...

How to show an array in AngularJS and also display any selected values?

Can someone assist me in displaying a list of data from an array on the loading page? Additionally, I need help with showing the selected value from a dropdown once it is chosen. <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...

Preventing the copying and pasting of HTML ruby tags

I am currently using ruby tags to include pinyin in my text. For instance: <p> <ruby>与<rt>yǔ</rt></ruby><ruby>摩<rt>mó</rt></ruby><ruby>拜<rt>bài</rt></ruby><ruby& ...

Material-UI's JSS does not adhere to the justify content property

Having trouble aligning elements in my React app using material-ui 1.0 and its JSS solutions. The code I've written below should center justify, but it's not working no matter how I configure it. I suspect there might be a naming issue since mat ...

Position the button to the left of the input field

I need to create a button with the id #searchBtn positioned to the left of an input form with the id #searchInput. HTML: header { position: relative; z-index: 2; } .searchInputMain { width: 300px; height: 40px; } .searchButton { width: 200px ...

how to prevent autoscrolling in an angular application when overflow-x is set to

In my socket event, I am using $scope.items.unshift(item) to place the new item at the top of the list. The html code includes <ol ng-repeat="item in items"><li>{{item.name}}</li></ol> An issue arises when a new item is added whil ...

Spin the text within a div by 90 degrees and align it to the right side

I have been attempting to create an information box that contains a simple info text div. On the right side of this info box, I want to include a text labeled "more info" within a nested div to signify that the info box is interactive. To save space, I wou ...