使用jQuery .scale从中心均匀地显示元素
Using jQuery .scale to evenly reveal elements from their center
拨动这里
问题描述:
我试图使用jQuery UI scale
的效果显示一个大的元素(div与背景图像)从它的中心点,并有它向外扩展均匀横跨屏幕。我想要的效果是让它看起来好像元素正在向你放大。
不幸的是,它向上和向左滑动,而不是均匀地使图像可见。确定它的起源和方向并不能解决问题。什么好主意吗?
您只需要使用答案中描述的技术来拉伸和缩放背景中的CSS图像-仅使用CSS。
例如,将background-size: 100% 100%;
规则添加到.box
div中:
// Small plugin to center any element.
jQuery.fn.center = function() {
this.css("position", "absolute");
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px");
this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
return this;
}
// Call the .center(); function to the ".box" div.
// This can be used to center anything. just use .center(); on any element.
$('.box').center();
// When the window is resized, center the div to adjust to the resize.
$(window).resize(function() {
$('.box').center();
});
// Now call the jQuery UI animation.
$('.box').show("scale", {
percent: 100
}, 1000, function() {
// on Animation Complete, do something.
});
.box {
width: 820px;
height: 461px;
background: url('http://www.techulator.com/attachments/Resources/10090-1015-Transforming-Windows-Iron-Man-s-JARVIS-Theme-Interface.png') no-repeat;
background-size: 100% 100%; /** this was added **/
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<div class="box"></div>
相关文章:
- javascript来显示元素属性
- 使用JavaScript或jQuery隐藏和显示元素
- 如何在Angular JS中滚动显示元素
- 如何在使用jQuery应用display:none后正确显示元素
- 仅使用Javascript隐藏/显示元素,而不会弄乱DOM
- jQuery单击并显示元素列表
- 键盘没有在Android和BlackBerry中显示元素焦点
- 在CSS转换期间显示元素的大小值
- 当我们关注jQuery时,只显示元素
- 包含方法和突出显示元素的问题
- JS/JQuery隐藏元素,更改文本,显示元素
- 如何显示元素的一部分,并在单击时切换到所有元素
- 在隐藏元素中显示元素
- 当PROTRACTOR元素后面有元素时,我怎么能知道当前的显示元素
- 在mouseover上显示元素在mouseleave上隐藏
- jquery显示元素是否具有类和特定值的内容
- 不能在复选框旁边显示元素
- 动态显示元素上的JavaScript触摸端;不要开火
- 点击JQuery/HTML5显示元素的索引/位置
- AngularJS:如何默认显示元素,并在按下按钮时切换