gpt4 book ai didi

jquery - 标题部分视差滚动效果 jQuery 和垂直中心边距

转载 作者:太空宇宙 更新时间:2023-11-04 13:01:52 25 4
gpt4 key购买 nike

我有一个带有背景图像的标题部分和一个带有 Logo 的元素。
现在这个标志在部分中间以 jQuery 为中心。
当我尝试向 Logo 添加视差效果时,它不再居中,因为我还使用了视差的边距变化。

这是我的代码,我还添加了一个 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",
});
}
});

最佳答案

与其将负边距的绿色框居中,不如试试这种居中方法:

section.head .inner {
position: relative;
top: 50%;
left: 50%;
background-color: green;
-webkit-transform:translate(-50%, -50%);
-moz-transform:translate(-50%, -50%);
-ms-transform:translate(-50%, -50%);
-o-transform:translate(-50%, -50%);
transform:translate(-50%, -50%);
}

您必须编辑 JS 以移除负边距。只需删除这些行:

$(".head .inner").css('margin-top',viewportHeightInnerMargin);
$(".head .inner").css('margin-left',viewportHeightInnerMargin);

关于jquery - 标题部分视差滚动效果 jQuery 和垂直中心边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25393648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com