gpt4 book ai didi

Javascript改变html元素的绝对位置

转载 作者:太空宇宙 更新时间:2023-11-03 19:26:59 25 4
gpt4 key购买 nike

我正在创建一个新站点,正如您将在下面的代码中看到的那样,我在背景固定位置图像上方放置了两个类( Logo 和文本)。我使用 position absolute 将它们放在图像的顶部,左边 50%,顶部 50% 和 transform: translate(-50%, -50%);这工作正常,没有问题,并且以我希望的方式响应。

但是,一旦我将我的 JS 代码 (fade_effect) 添加到站点,它就会稍微移动两个元素( Logo 和文本)的位置。有人可以帮助我,已经多次编辑代码并进行研究但找不到解决方案。我相信这可能与我阅读后的 JS 代码部分 .offset() 有关,这似乎是影响这些元素位置的 JS 代码部分。请看下面的代码:

HTML:

<link rel="stylesheet" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.min.css" />

<body style="height: 2000px; margin: 0; padding: 0;">

<div class="img_slide">
<h1 class="logo fade_effect">Logo Here</h1>
<p class="text fade_effect">Welcome to My Site</p>
</div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

CSS:

/** Background image code BELOW **/
.img_slide {
height: 800px;
width: 100%;
background-image: linear-gradient(150deg, rgba(68, 0, 139, 0.7), rgba(47, 0, 120, 0.6) 10%, rgba(27, 57, 129, 0.5) 30%, rgba(41, 123, 192, 0.5) 50%, rgba(114, 240, 255, 0.6) 86%, rgba(84, 183, 249, 0.7)), url("http://www.strawberryproofing.co.uk/images/nature_large1.png");
background-repeat: no-repeat;
background-position: 0%, 0%, 50%, 50%;
background-attachment: scroll, fixed;
background-size: auto, cover;
}
/** Background image code ABOVE **/


.logo {
font-family: 'Raleway', sans-serif;
font-weight: normal;
font-size: 60px;
color: white;
padding: 25px;
border: 3px solid white;
/* position absolute - center */
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);

}

.text {

color: white;
font-family: 'Raleway', sans-serif;
font-weight: normal;
font-size: 25px;
padding: 10px;
/* position absolute - center */
position: absolute;
left: 50%;
top: 70%;
transform: translate(-50%, -50%);

}



/** CSS code called by the JS .addClass **/

.fade_effect {

opacity: 0;

}

.FadeIn {
-webkit-animation: slideIn 0.8s ease 0.3s forwards;
animation: slideIn 0.8s ease 0.3s forwards;
}

@-webkit-keyframes slideIn {
0%{
-webkit-transform: translateY(40px);
opacity: 0;
}
100% {
-webkit-transform: translateY(0px);
opacity: 1;
}

}


@keyframes slideIn {
0% {
-webkit-transform: translateY(40px);
opacity: 0;
}
100% {
-webkit-transform: translateY(0px);
opacity: 1;
}
}

/** CSS code called by the JS .addClass **/

Javascript:

var $fade =  $(".fade_effect"); //Calling the class in HTML

$(window).scroll(function () { //Using the scroll global variable
$fade.each(function () {

fadeMiddle = $(this).offset().top + (0.4 *$(this).height());
windowBottom = $(window).scrollTop() + $(window).height();

if (fadeMiddle < windowBottom) {
$(this).addClass("FadeIn");
}
});
});

/* On Load: Trigger Scroll Once*/
$(window).scroll();

这也可以通过我的 CodePen 页面在线查看:https://codepen.io/billyfarroll/pen/qYWYYV

最佳答案

请看下面的代码:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.min.css" />
<style>
/** Background image code BELOW **/
.img_slide {
height: 800px;
width: 100%;
background-image: linear-gradient(150deg, rgba(68,
0, 139, 0.7), rgba(47, 0, 120, 0.6) 10%, rgba(27, 57, 129, 0.5) 30%, rgba(41, 123, 192, 0.5) 50%, rgba(114, 240, 255, 0.6) 86%, rgba(84, 183, 249, 0.7)), url("http://www.strawberryproofing.co.uk/images/nature_large1.png");
background-repeat: no-repeat;
background-position: 0%, 0%, 50%, 50%;
background-attachment: scroll, fixed;
background-size: auto, cover;
}
/** Background
image code ABOVE **/
.logocontainer {
color: white;
padding: 0 20px;
border: 3px solid white;
position: absolute;
left: 50%;
top: 50%;
}
.logo {
font-family: 'Raleway', sans-serif;
font-weight: normal;
font-size: 60px;
color: white;
padding: 25px;
text-align: center;
}
.text {
color: white;
font-family: 'Raleway', sans-serif;
font-weight: normal;
font-size: 25px;
padding: 10px;
/* position absolute - center */
position: absolute;
left: 50%;
top: 70%;
/* transform: translate(-50%, -50%); */
}
/** CSS
code called by the JS .addClass **/
.fade_effect {
opacity: 0;
}
.FadeIn {
-webkit-animation: slideIn 0.8s ease 0.3s forwards;
animation: slideIn 0.8s ease 0.3s forwards;
}
@-webkit-keyframes slideIn {
0% {
-webkit-transform: translate(0, 40px);
opacity: 0;
}
100% {
-webkit-transform: translate(-50%, -50%);
opacity: 1;
}
}
@keyframes slideIn {
0% {
-webkit-transform: translate(0, 40px);
opacity: 0;
}
100% {
-webkit-transform: translate(-50%, -50%);
opacity: 1;
}
}
/** CSS code called by the JS .addClass **/
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body style="height: 2000px; margin: 0; padding: 0;">
<div class="img_slide">
<div class="logocontainer fade_effect">
<h1 class="logo">
Logo Here
</h1>
<p class="text fade_effect">
Welcome to My Site
</p>
</div>
</div>
<script>
var $fade = $(".fade_effect");
//Calling the class in HTML
$(window).scroll(function () {
//Using the scroll global variable
$fade.each(function () {
fadeMiddle = $(this).offset().top + (0.4 * $(this).height());
windowBottom = $(window).scrollTop() + $(window).height();
if (fadeMiddle < windowBottom) {
$(this).addClass("FadeIn");
}
}
);
}
);
/* On Load: Trigger Scroll Once*/
$(window).scroll();
</script>
</body>
</html>

关于Javascript改变html元素的绝对位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49954873/

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