有人能解释一下为什么我的CSS转换如此突然吗?

ou6hu8tu  于 2023-05-02  发布在  其他
关注(0)|答案(1)|浏览(107)

我有一个searchbar写在react像这样:

import './search.css'
const SearchBar = ({searchQuery, setSearchQuery}) => (
    <div className="search">      
        <input
            value = {searchQuery}
            onInput = {e => setSearchQuery(e.target.value)}
            type = "text"
            placeholder = "Search driver apps..."
            />
            <div className = "inSearch"></div>
    </div>
);

我的CSS看起来像这样:

@import url('https://fonts.googleapis.com/css?family=Inconsolata:700');

.search{
  position: relative;
  margin-top: 50px;
  margin-left: 30px;
  width: 300px;
  height: 100px;
}

.search .inSearch
{
    /*width: 140px;
    opacity: 1;
    margin-left: 50px;
    margin-top: 100px;
    outline: auto;*/

   position: absolute;
     margin-top: 30px;
   margin-left: 0px;
     width: 70px;
     height: 70px;
     background: #faa81a;
     border-radius: 50%;
   transition: all 1s ease-out;
     z-index: 4;
     box-shadow: 0 0 25px 0 rgba(0, 0, 0, 0.4);
   opacity: 1;
}

.search .inSearch:hover{
  cursor: pointer;
}

.search .inSearch::before{
  content: "";
  position: absolute;
  margin: auto;
  top: 22px;
  right: 0;
  bottom: 0;
  left: 22px;
  width: 12px;
  height: 2px;
  background: white;
  transform: rotate(45deg);
  transition: all 0.5s ease-out;
}

.search .inSearch::after{
   content: "";
     position: absolute;
     margin: auto;
     top: -5px;
     right: 0;
     bottom: 0;
     left: -5px;
     width: 25px;
     height: 25px;
     border-radius: 50%;
     border: 2px solid white;
     transition: all .5s;
}

.search input {
  font-family: 'Inconsolata', monospace;
  position: absolute;
  margin-top: 30px;
  margin-left: 0px;
  width: 60px;
  height: 60px;
  outline: none;
  border: none;
  background: #faa81a;
  color: white;
  text-shadow: 0 0 10px #faa81a;
  padding: 0 80px 0 20px;
  border-radius: 30px;
  box-shadow: 0 0 25px 0 #faa81a, 0 20px 25px 0 rgba(0, 0, 0, 0.2);
  transition: all 1s ease-out;
  opacity: 0;
  z-index: 5;
  font-weight: bolder;
  letter-spacing: 0.1em;
}

.search input:hover{
  cursor: pointer;
}

.search input:focus{
   height: 50px;
   width: 200px;
     opacity: 1;
     cursor: text;
}

.search input:focus ~ .inSearch {
   right: -20px;
     background: #151515;
     z-index: 6;
}

.search input:focus ~ .inSearch::before{
  top: 0;
  left: 0;
     width: 25px;
}

.search input:focus ~ .inSearch::after{
  top: 0;
  left: 0;
  width: 25px;
  height: 2px;
  border: none;
  background: white;
  border-radius: 0%;
  transform: rotate(-45deg);
}

.search input::placeholder {
  color: white;
  opacity: 0.5;
  font-weight: bolder;
}

我对99%的转换感到满意,但我不明白为什么inSearch div向右移动得如此之快,而搜索栏却在左边追赶。我想让它们同时向外移动。我试过缓和过渡,我试过线性,延长时间,延迟,但似乎无论我改变什么,它只是跳到右边。

myzjeezk

myzjeezk1#

要转换.search input:focus ~ .inSearch规则中的right属性,需要知道从哪里开始。因此,您需要将right属性添加到处于非转换状态的.insearch元素中。将right添加到.search .inSearch并解决此问题。代码如下:

@import url("https://fonts.googleapis.com/css?family=Inconsolata:700");

.search {
  position: relative;
  margin-top: 50px;
  margin-left: 30px;
  width: 300px;
  height: 100px;
}

.search .inSearch {
  /*width: 140px;
    opacity: 1;
    margin-left: 50px;
    margin-top: 100px;
    outline: auto;*/
  right: 80%; /* added this */
  position: absolute;
  margin-top: 30px;
  margin-left: 0px;
  width: 70px;
  height: 70px;
  background: #faa81a;
  border-radius: 50%;
  transition: all 1s ease-out;
  z-index: 4;
  box-shadow: 0 0 25px 0 rgba(0, 0, 0, 0.4);
  opacity: 1;
}

.search .inSearch:hover {
  cursor: pointer;
}

.search .inSearch::before {
  content: "";
  position: absolute;
  margin: auto;
  top: 22px;
  right: 0;
  bottom: 0;
  left: 22px;
  width: 12px;
  height: 2px;
  background: white;
  transform: rotate(45deg);
  transition: all 0.5s ease-out;
}

.search .inSearch::after {
  content: "";
  position: absolute;
  margin: auto;
  top: -5px;
  right: 0;
  bottom: 0;
  left: -5px;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  border: 2px solid white;
  transition: all 0.5s;
}

.search input {
  font-family: "Inconsolata", monospace;
  position: absolute;
  margin-top: 30px;
  margin-left: 0px;
  width: 60px;
  height: 60px;
  outline: none;
  border: none;
  background: #faa81a;
  color: white;
  text-shadow: 0 0 10px #faa81a;
  padding: 0 80px 0 20px;
  border-radius: 30px;
  box-shadow: 0 0 25px 0 #faa81a, 0 20px 25px 0 rgba(0, 0, 0, 0.2);
  transition: all 1s ease-out;
  opacity: 0;
  z-index: 5;
  font-weight: bolder;
  letter-spacing: 0.1em;
}

.search input:hover {
  cursor: pointer;
}

.search input:focus {
  height: 50px;
  width: 200px;
  opacity: 1;
  cursor: text;
}

.search input:focus ~ .inSearch {
  right: -20px;
  background: #151515;
  z-index: 6;
}

.search input:focus ~ .inSearch::before {
  top: 0;
  left: 0;
  width: 25px;
}

.search input:focus ~ .inSearch::after {
  top: 0;
  left: 0;
  width: 25px;
  height: 2px;
  border: none;
  background: white;
  border-radius: 0%;
  transform: rotate(-45deg);
}

.search input::placeholder {
  color: white;
  opacity: 0.5;
  font-weight: bolder;
}
<div class="search">
  <input value="searchQueryd" type="text" placeholder="Search driver apps..." >
  <div class="inSearch"></div>
</div>

相关问题