javascript - 无法从具有显示的父 div 获取“左”值:flex
问题描述
所以我试图在单击按钮时制作背景幻灯片动画,但我似乎无法从下面的代码中获得“left”的确切值,显然我在这里做错了......
我试图使“.btn”位置:静态/相对,两者都导致“左”=“自动”或“0”。
尝试过 offset()、position(),对我不起作用。任何帮助都感激不尽!
$(".btn").click(function(){
var pos_left = $(this).css("left");
$(".result").html(pos_left);
//$(".animation").css("left", pos_left); (THIS IS EXPECTED)
$(".animation").css("left", 35);
});
.wrapper {
position: relative;
}
.container {
display: flex;
width: min-content;
justify-content: center;
background: rgba(255, 255, 255, 0);
border: 2px black solid;
}
.btn {
background-color: rgba(255, 255, 255, 0);
color: green;
max-width: 110px;
width: 100%;
height: 30px;
font-size: 12px;
border: 2px green solid;
border-color: yellowgreen;
white-space: nowrap;
}
.animation {
position: absolute;
top: 0px;
left: 0px;
height: 100%;
width: 34px;
background: red;
z-index: -1;
transition: 0.4s;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div class="wrapper">
<div class="container">
<div class="btn">Box 1</div>
<div class="btn">Box 2</div>
<div class="btn">Boxie 3</div>
<div class="btn">Boxie box 4</div>
</div>
<div class="animation"></div>
</div>
<div class="result"></div>
解决方案
I simply try to get your code work as naively as possible like this:
$(".btn").click(function(){
var position = $(this).position();
var pos_left = position.left;
var elementWith = $(this).outerWidth();
$(".result").html(pos_left);
$(".animation").css("left", pos_left);
$(".animation").width(elementWith)
});
.wrapper {
position: relative;
}
.container {
display: flex;
width: min-content;
justify-content: center;
background: rgba(255, 255, 255, 0);
border: 2px black solid;
}
.btn {
background-color: rgba(255, 255, 255, 0);
color: green;
max-width: 110px;
width: 100%;
height: 30px;
font-size: 12px;
border: 2px green solid;
border-color: yellowgreen;
white-space: nowrap;
}
.animation {
position: absolute;
top: 0px;
left: 0px;
height: 100%;
width: 34px;
background: red;
z-index: -1;
transition: 0.4s;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div class="wrapper">
<div class="container">
<div class="btn">Box 1</div>
<div class="btn">Box 2</div>
<div class="btn">Boxie 3</div>
<div class="btn">Boxie box 4</div>
</div>
<div class="animation"></div>
</div>
<div class="result"></div>
推荐阅读
- java - 表java。没有数组
- perl - 如何检查文件内容并根据检查条件添加或更新文件?
- php - $rowcount=mysqli_num_rows($result); 错误没有原因
- php - 如何使用 WordPress 编辑图像
- ios - 自动屏幕录制 xcrun 还创建了 2 个像素的视频
- c# - 如何在 C# 中使用正则表达式从字符串中提取多个子字符串
- swift - 如何安全地允许每个人使用 Firebase 实时数据库读取和授权用户写入应用程序?
- css - 使用 css 选择前 5 个元素,但当有特定的类名时跳过
- python - Python3 Pandas 根据内部条件替换行
- javascript - 在标签下方移动时如何让我的滑块改变颜色?