简体   繁体   English

如何在两个html页面之间做过渡效果

[英]How to do transition effects between two html pages

I need to transition effect for between the html pages, when clicking menu or submenu's, page will be opened with transition effect.我需要在 html 页面之间进行过渡效果,当单击菜单或子菜单时,页面将打开带有过渡效果。 Please guide me, how to do that, Thanks in advance.请指导我,如何做到这一点,提前致谢。

The below link is only have div transition not a page transition.下面的链接只有 div 过渡而不是页面过渡。 it is possible to the same transition between html pages? html页面之间可以进行相同的转换吗?

How I can add transitions between two HTML pages? 如何在两个 HTML 页面之间添加过渡?

Here is a solution that requires some knowledge of CSS and Javascript:这是一个需要一些 CSS 和 Javascript 知识的解决方案:

In your DOM, where you put your links to the other pages, instead of using <a> tags, use an ordinary <span> and attach an onclick attribute, like this:在您的 DOM 中,您将链接放置到其他页面,而不是使用<a>标签,而是使用普通的<span>并附加一个 onclick 属性,如下所示:

<span onclick="transitionToPage('https://www.google.com')"></span>

(You can use <a> and href , but you need to parse out the target href and prevent the default event.) (您可以使用<a>href ,但您需要解析出目标 href 并阻止默认事件。)

Then in your Javascript, put this code:然后在您的 Javascript 中,输入以下代码:

window.transitionToPage = function(href) {
    document.querySelector('body').style.opacity = 0
    setTimeout(function() { 
        window.location.href = href
    }, 500)
}

document.addEventListener('DOMContentLoaded', function(event) {
    document.querySelector('body').style.opacity = 1
})

Finally, in your CSS code:最后,在您的 CSS 代码中:

body { 
   opacity: 0;
   transition: opacity .5s;
}

This will have the following effect:这将产生以下效果:

  1. When page loads, the body will fade in over half a second页面加载时,正文会在半秒以上淡入淡出
  2. When you click on a link, the body will fade out and after half a second the browser will go to the next page当你点击一个链接时,正文会淡出,半秒后浏览器将转到下一页

Happy coding!快乐编码!

You can always hide content of body and then fade it via JQuery.您始终可以隐藏body内容,然后通过 JQuery 将其淡出。 But that will work only in the case of entering page.但这仅在进入页面的情况下有效。 If you want to add animation to leaving page then you have to go with Single Page Application, where you can add transitions at both levels.如果您想为离开页面添加动画,那么您必须使用单页应用程序,您可以在其中添加两个级别的过渡。

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

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