gpt4 book ai didi

javascript - 具有绝对定位的 Flex 在 Safari 中不起作用

转载 作者:行者123 更新时间:2023-11-30 11:44:44 24 4
gpt4 key购买 nike

我正在尝试用 flexbox 制作一个文本 slider 。

我希望问题 div 下的所有子元素都像您在 chrome 中看到的那样折叠。

在实际代码中,非事件元素将设置为 translateX(100%),事件索引元素将设置为 0%。

我想使用 flexbox 的原因是 Ques*: 应该与问题文本的第一行对齐,并且 text-container div 应该居中question div 无论q-text div 的长度是多少。(在没有 flex 的情况下尝试过,但我无法对齐 Quest*first line of 文本)

与示例代码一样,由于文本长度不同,中心位置也不同。

在 Chrome 中它工作正常。 但是,它不以 Safari 为中心(使用最新版本的 Safari)。

如果有更好的方法来实现这一点,我很高兴看到!

#container {
width: 100%;
height: 200px;
background: black;
}

.question {
display: -webkit-flex;
display: flex;
height: 100%;
width: 100%;
overflow: hidden;
justify-content: center;
align-items: center;
-webkit-justify-content: center;
-webkit-align-items: center;
}

.text-container {
display: -webkit-flex;
display: flex;
width: 100%;
position: absolute;
color: white
}

.q {
width: 25%;
display: flex;
justify-content: center;
align-items: center;
-webkit-justify-content: center;
-webkit-align-items: center;
align-self: baseline;
}
.q-text {
width: 75%;
padding-right: 12%;
}
<html>

<body>

<div id="container">
<div class="question">

<div class="text-container">
<div class="q">
Qest1:
</div>
<div class="q-text">
1Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type an
</div>

</div>
<div class="text-container">
<div class="q">
Qest2:
</div>
<div class="q-text">
2Lorem Ipsum is simply dummy text of the printing and ty
</div>

</div>
<div class="text-container">
<div class="q">
Qest3:
</div>
<div class="q-text">
3Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when
</div>

</div>

</div>

</div>
</body>

</html>

text 1

text 2

最佳答案

这里有一个选项,使用display: inline-block代替flexbox,和transform: translate

window.addEventListener('load', function() {
document.querySelector('button').addEventListener('click', function() {
var ques = document.querySelector('.text-container:not(.hidden)');
ques.classList.toggle('hidden');
var next = ques.nextElementSibling;
if (next) {
next.classList.toggle('hidden');
return;
}
document.querySelector('.text-container').classList.toggle('hidden');
})
})
.container {
height: 160px;
background: black;
}
.question {
position: relative;
margin: 0 auto;
width: 90%;
height: 100%;
overflow: hidden;
}
.text-container {
position: absolute;
top: 50%;
left: 50%;
width: 100%;
transform: translate(-50%,-50%);
color: white;
transition: left 0.5s;
}
.text-container.hidden {
left: -50%;
}
.q {
display: inline-block;
width: 20%;
vertical-align: top;
}
.q-text {
display: inline-block;
width: 80%;
vertical-align: top;
padding-right: 12%;
box-sizing: border-box;
}
button {
margin: 15px 0;
padding: 10px;
}
<div class="container">
<div class="question">

<div class="text-container">
<div class="q">
Qest1:
</div><div class="q-text">
1Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type an
</div>
</div>

<div class="text-container hidden">
<div class="q">
Qest2:
</div><div class="q-text">
2Lorem Ipsum is simply dummy text of the printing and ty
</div>
</div>

<div class="text-container hidden">
<div class="q">
Qest3:
</div><div class="q-text">
3Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when
</div>
</div>

</div>
</div>
<button>Next question</button>

关于javascript - 具有绝对定位的 Flex 在 Safari 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41402010/

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