Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autohide an element in VueJS after 1 second

OK, so I'm new to Vue ( basically, new to JS in general, but I'm playing with Vue right now ) and what I want to do, is to auto hide an element ( not on click ) inside the template tag of a component. In jQuery this would look like:

$(function() {
    setTimeout(function() {
        $(".hideElement").hide()
    }, 1000);
});

but how this works in Vue? my component looks like this:

<template>
<div>
 <h1 class="hideElement"> HELLO </h1>
</div>
</template>

<script> // nothing here 
</script>

<style> // nothing here
</style>

I know how to toggle the element on click of a button, but I just want to auto hide it after 1 second without any click events everytime the users enter this component ( which is a new "page" )

like image 847
Policsek Avatar asked Mar 15 '19 18:03

Policsek


People also ask

How do I hide a div after some time?

Select the div element. Use delay function (setTimeOut(), delay()) to provide the delay to hide() the div element.

What does $refs mean in Vue?

Feb 3, 2020. The $refs property in Vue is used to reference DOM elements in the Vue instance's templates. A common use case for $refs is focusing on a DOM element when a certain event happens.

What does $t mean in Vue?

Vuex is a state management pattern for vue. js. $t is the injected method from vue. js or Vue. i18n.

What does $Set do in Vue?

$set is "For Vue instance" while Vue. set is "For plain data objects" and that Vue. set is global: However, there are ways to add a property and make it reactive after an instance has been created.


Video Answer


1 Answers

You could just add a property in the data object and use v-show directive to determine whether the element should be visible or not. If the boolean is false the element is hidden, if true the element is visible.

Method Created called synchronously after the instance is created.

<template>
    <div>
        <h1 v-show="elementVisible" class="hideElement"> HELLO </h1>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                elementVisible: true
            }
        },

        created() {
            setTimeout(() => this.elementVisible = false, 1000)
        }
    }
</script>
like image 180
Martin Štefek Avatar answered Sep 19 '22 15:09

Martin Štefek