简体   繁体   English

为什么我的元素不会消失?

[英]Why wont my elements fade in?

So far my website that I am doing only animates text that is specified in "var txt". 到目前为止,我正在执行的网站仅对“ var txt”中指定的文本设置动画。

My issue is trying to get the following elements(an image and some text) to fade in after the first function (typeWriter) is finished executing. 我的问题是尝试在第一个函数(typeWriter)执行完毕后淡入以下元素(图像和一些文本)。

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    var i = 0;
    var txt = 'Test';
    var speed = 50;
    window.onload = function typeWriter(callback){
        if (i < txt.length){
        document.getElementById("type").innerHTML += txt.charAt(i);
        i++;
        setTimeout(function(){typeWriter(callback)}, speed);
        }else if(typeof callback == 'function'){
         callback();
        }
    }
    function fader(){
        $("#fade").fadeIn("slow");
    }

    typeWriter(callback);

<!-- Html below -->

    <div class="subSection">
    <div id="fade">
        <h2 id="type"></h2>
        <img src="ring.jpg" alt="rings">
        <p>Now comes in a range of different colours and sizes.<br>Pricing from €149.99</p>
        <p><a href=order.html class="button">Buy Now</a></p>
    </div>
</div>

@Noface was faster than me, but since my solution is different, I still feel like posting it. @Noface比我快,但是由于我的解决方案不同,所以我仍然想发布它。 The major difference is that it does not expose global variables. 主要区别在于它不公开全局变量。 That means that you can have multiple instances of it running on a page. 这意味着您可以在一个页面上运行它的多个实例。

Here you go: 干得好:

 function typeWriter(options) { // Getting options or using default values var settings = { element: options.element, text: options.text || "Typewriter", speed: options.speed || 100, complete: options.complete || function() {} }; // Internal state var state = { i: 0, len: settings.text.length }; (function writeLetter() { if (state.i >= state.len) return settings.complete(); settings.element.innerHTML += settings.text.charAt(state.i); state.i++; setTimeout(writeLetter, settings.speed); })(); } function fader() { $("#fade").fadeIn("slow"); } window.onload = function() { // Call the function with options typeWriter({ element: document.getElementById("type"), text: "Hello world!", speed: 50, complete: fader }); }; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h2 id="type"></h2> <div id="fade" style="display: none">I'm fading!</div> 

After fixing the intial syntax errors, there were some issues to solve. 固定INTIAL语法错误之后,有一些问题需要解决。 The onload will fire and pass its own arguments, not the callback you want. onload将触发并传递自己的参数,而不是您想要的回调。 So instead wrap it in a callback function and call the function with the chosen callback. 因此,而不是包装在一个回调函数调用函数与所选择的回调。 Alternatively use onload = typeWriter.bind(null, fader) which passes fader as the callback function. 或者使用onload = typeWriter.bind(null, fader) ,它将推子作为回调函数传递。 Next you want to set the #fade element to display: none in your css so it starts hidden, and then move your txt #type outside the fade div, so it is visible and you can see the auto-typing animation you made. 接下来,你要设定的#fade元素display: none在你的CSS所以它开始隐藏,然后将您的TXT #type渐变格之外,因此它是可见的,你可以看到你所做的自动打字动画。

You can define the typeWriter as a function, and then call it in the window.onload event: 您可以将typeWriter定义为一个函数,然后在window.onload事件中调用它:

 var i = 0; var txt = 'Test'; var speed = 50; function typeWriter(callback){ if (i < txt.length){ document.getElementById("type").innerHTML += txt.charAt(i); i++; setTimeout(function(){typeWriter(callback)}, speed); } else if(typeof callback == 'function'){ callback(); } } function fader(){ $("#fade").fadeIn("slow"); } window.onload = function(){ typeWriter(fader); } 
 #fade { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="subSection"> <h2 id="type"></h2> <div id="fade"> <img src="ring.jpg" alt="rings"> <p>Now comes in a range of different colours and sizes.<br>Pricing from €149.99</p> <p><a href=order.html class="button">Buy Now</a></p> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM