[英]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. 结果真的很奇怪。
So obviously this technique doesn't work: 因此,显然,此技术不起作用:
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: 再次,这只是以下情况:图像缩小(使用
transform
的scale
),具有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中这样做:
$(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.