javascript - 使用 z-index 剪切标签

标签 javascript jquery html css z-index

我将垂直标签“PINK”对齐在部分的中间。 当我向下滚动到下一部分时,“PINK”将被具有更高 z-index 的下一部分覆盖。

div.back1 {
    background-color: #FF00FF;
    position: relative;
    width: 100%;
    height: 2000px;
    z-index: 10;
}
div.text1 {
	transform: rotate(-90deg);
    position: fixed;
    top: 50%;
    background-color: #FFFFFF;
    z-index: 20;
}
div.back2 {
    background-color: #0000FF;
    position: relative;
    width: 100%;
    height: 2000px;
    z-index: 30;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<div class="text1">PINK</div>
<div class="back1"></div>
<div class="back2"></div>

</body>
</html>

我希望第二部分中出现第二个标题“BLUE”,如以下模型所示。

enter image description here

是否可以通过排列 z 索引来实现此结果? 是否有另一种更好的方法来裁剪标签,使其与视口(viewport)的 50% 对齐?

提前非常感谢您的贡献!

最佳答案

尝试此代码

既然你标记了jquery,我就用它来添加类fixed

$(document).ready(function(){
$('.blue-text').hide();
$(window).scroll(function() {
if ($('.blue').offset().top <= $('.pink-text').offset().top + 40)
{
  $('.blue-text').show();
  if($('.blue').offset().top <= $('.pink-text').offset().top){
     $('.blue-text').addClass('fixed');
  }
}
else{
  $('.blue-text').removeClass('fixed');
  $('.blue-text').hide();
}
});
});
div.back1 {
    background-color: #FF00FF;
    position: relative;
    width: 100%;
    height: 2000px;
    z-index: 10;
}
div.text1.fixed {
	transform: rotate(-90deg);
    position: fixed;
    top: 50%;
    background-color: #FFFFFF;
    z-index: 20;
}
div.back2 {
    background-color: #0000FF;
    position: relative;
    width: 100%;
    height: 2000px;
    z-index: 30;
}
.blue,.pink {
    position: relative;
}
.text1 {
    position: absolute;
    top: 10px;
    z-index: 31;
    transform: rotate(-90deg);
    background-color: #FFFFFF;
}
.text1.blue-text.fixed{
    z-index: 31;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="color">
<div class="pink">
<div class="text1 pink-text fixed">PINK</div>
<div class="back1"></div>
</div>
<div class="blue">
<div class="text1 blue-text">BLUE</div>
<div class="back2"></div>
</div>
</div>

关于javascript - 使用 z-index 剪切标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44453505/

相关文章:

javascript - 你能在 TypeScript 中创建嵌套类吗?

html - 使用@media 设置的着色填充区域

html - 如何在 Angular2 中使用 *ngFor 动态生成 ngModel?

javascript - SVG渐变色

javascript - 在子窗口上绑定(bind) jQuery 事件

JavaScript 检测 AJAX 事件

javascript - 不存在的 XML 节点破坏了我的脚本

jquery - `.live()` 和 `.autocomplete()` 之间不一致

jquery - jqGrid - 通过 Json 动态添加行

jquery - 在 URL 哈希更改上应用 CSS 类