【发布时间】:2014-10-13 03:46:53
【问题描述】:
我有一个带有背景图像的标题部分和一个带有徽标的元素。
现在这个标志以 jQuery 为中心,位于该部分的中间。
当我尝试为徽标添加视差效果时,它不再居中,因为我还为视差使用了边距变化。
这是我的代码,我还添加了一个 JS Fiddle 链接(视差效果在编辑器 iframe 中不起作用,因此您必须在新窗口中打开示例。
JS Fiddle Editor
JS Fiddle Show
代码:
// Viewport Height for head section
function viewport_height() {
var height = $(window).height();
var viewportHeight = (50 * height) / 100;
var viewportHeightInner = (50 * height) / 150;
var viewportHeightInnerMargin = (50 * height) / 300;
viewportHeight = parseInt(viewportHeight) + 'px';
viewportHeightInnerMargin = parseInt('-' + viewportHeightInnerMargin) + 'px';
$(".head").css('height',viewportHeight);
$(".head .background").css('height',viewportHeight);
$(".head .inner").css('height',viewportHeightInner);
$(".head .inner").css('width',viewportHeightInner);
$(".head .inner").css('margin-top',viewportHeightInnerMargin);
$(".head .inner").css('margin-left',viewportHeightInnerMargin);
}
// Resize head section on scale
$(document).ready(function() {
viewport_height();
$(window).bind('resize', viewport_height);
});
// Logo Parallax scroll
$(window).scroll(function(e){
if ($(window).width() > 800) {
$('.head .inner').css({
'margin-top' : +($(this).scrollTop()/2)+"px",
});
}
});
【问题讨论】:
标签: jquery html css margin parallax