简体   繁体   English

加载页面时的缩放效果无法正常运行

[英]Zoom Effect when Page is Loaded Doesn't Work as Wanted

I am trying to get an effect which is to zoom in on a logo centred on the page when the page is loaded. 我正在尝试获得一种效果,即在加载页面时放大位于页面中心的徽标。 I am using the following HTML and JS code: 我正在使用以下HTML和JS代码:

<div style="display: table-cell; vertical-align: middle; text-align: center;">
    <img id="logo" src="images/logo2.png" style="zoom: 200%; transition: zoom 1s ease-in-out;"/>
</div>

JS JS

document.addEventListener("load", pageFullyLoaded, true);

function pageFullyLoaded()
{
    var elem = document.getElementById("logo");
    elem.style.zoom = "300%";
}

The result is really odd. 结果真的很奇怪。

  1. It display the logo in it's normal size, 它以正常大小显示徽标,
  2. then it jumps on a super zoomed in version of the logo (> 1000%), 然后跳转到徽标的超级放大版本(> 1000%),
  3. zoom in on the logo even more (1000% to 1500% say) for the duration of the transition, 在过渡期间将徽标进一步放大(例如说从1000%到1500%),
  4. jump back to the normal logo size and position (which is correct, and this is the final positon and size I want). 跳回正常的徽标大小和位置(正确,这是我想要的最终位置和大小)。

So obviously this technique doesn't work: 因此,显然,此技术不起作用:

  1. the jump at the beginning is ugly but I only suppose this happens because 2) is incorrect anyway. 一开始的跳转很难看,但我只想这是因为2)仍然是错误的。 As it should start by default with a zoom value of 200% (which is defined in the style of the div) and then the JS should make it zoom to 300%. 因为默认情况下它应该以200%的缩放值开始(以div的样式定义),然后JS应该将其缩放到300%。 So there should be no jump visible really. 因此,实际上不应有任何可见的跳跃。
  2. I don't understand why I get this incredibly zoomed in version of the logo at the start of the animation. 我不明白为什么在动画开始时会如此放大徽标的版本。 Basically it's almost like if the entire image was filling up the screen. 基本上,几乎就像整个图像都填满了整个屏幕一样。

Any idea on how to do this reliably, please? 请问如何可靠地做到这一点? Thank you. 谢谢。

If you're looking to scale an image, you don't need to use zoom or transform or anything. 如果您要缩放图像,则无需使用缩放或变换之类的方法。 Just alter the width directly and the browser will scale the image for you: 只需直接更改宽度,浏览器就会为您缩放图像:

http://jsfiddle.net/C4JZv/ http://jsfiddle.net/C4JZv/

HTML: HTML:

<div class="wrapper">
    <img class="tiny" src="http://placehold.it/200x150" />
</div>

CSS: CSS:

.wrapper {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}

.wrapper img {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    transition: width 1s ease-in-out;
    width: 200px;
}

.wrapper img.tiny {
    width: 10px;
}

JS: JS:

document.querySelector('.wrapper img').className = "";

EDIT : You mentioned in the comments that you wanted to see this done using transform . 编辑 :您在评论中提到,您希望使用transform完成此操作。 Again, it's just a case of having a shrunken image (using transform 's scale ), having a transition property and then removing the CSS class that shrinks the image: 再次,这只是以下情况:图像缩小(使用transformscale ),具有transition属性,然后删除缩小图像的CSS类:

http://jsfiddle.net/C4JZv/1/ http://jsfiddle.net/C4JZv/1/

HTML & JS: Same HTML和JS:相同

CSS: Mostly the same, but with a couple of changes (plus a load of vendor prefixes): CSS:大致相同,但有几处更改(加上大量的供应商前缀):

.wrapper img {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    -webkit-transition: -webkit-transform 1s ease-out;
    -moz-transition: -moz-transform 1s ease-out;
    -ms-transition: -ms-transform 1s ease-out;
    -o-transition: -o-transform 1s ease-out;
    transition: transform 1s ease-out;
}

.wrapper img.tiny {
    -moz-transform: scale(0.1);
    -webkit-transform: scale(0.1);
    -o-transform: scale(0.1);
    -ms-transform: scale(0.1);
    transform: scale(0.1);
}

I would do this in only CSS like so: 我只会在CSS中这样做:

  1. Set the image to scaleX and scaleY 0 (or hide it in some other way) 将图像设置为scaleX和scaleY 0(或以其他方式隐藏)
  2. On $(window).load or $('document').ready add a class with keyframe animations 在$(window).load或$('document')。ready上添加带有关键帧动画的类
  3. Do whatever you need afterwards. 然后做您需要的任何事情。

Fiddle 小提琴

$(window).load(function(){
    $('img.zoom').addClass('element-animation');
});

You can also listen to animation end events like so https://github.com/daneden/animate.css#usage 您也可以收听动画结束事件,例如https://github.com/daneden/animate.css#usage

That library (Animate.css) is also pretty handy and you might be able to find some useful effects in it. 该库(Animate.css)也非常方便,您也许可以在其中找到一些有用的效果。

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

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