Skip to content

Commit

Permalink
优化SearchTop并完成input组件
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-LinPromise committed Aug 17, 2022
2 parents dd56f15 + 22af015 commit 8ae5f4a
Show file tree
Hide file tree
Showing 6 changed files with 992 additions and 715 deletions.
35 changes: 23 additions & 12 deletions src/Components/SearchMain/index.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React,{useEffect} from 'react';
import React,{useEffect,useState} from 'react';
import "./style.scss"

const SearchMain = () => {
const Distance=[]
const [domLeft,setDomLeft]=useState()
useEffect(()=>{
let items=document.querySelectorAll(".Toggle")
let scroll=document.querySelector(".Toggle-scroll")
setDomLeft(items[0].offsetLeft)
for(let i=0;i<items.length;i++){
Distance.push(items[i].offsetLeft)
items[i].addEventListener("click",function (){
scroll.style.left=(680+i*114)/1920*100+"rem"
scroll.style.left=Distance[i]+"px"
console.log(scroll.style.left)
for(let i=0;i<items.length;i++){
items[i].style.color="#d1d1d0"
Expand All @@ -20,17 +24,24 @@ const SearchMain = () => {
},[])
return (
<div className="SearchMain">
<ul className="Toggle-Top">
<li className="Toggle-usually Toggle" style={{color:"#FFFFFF"}}>常用</li>
<li className="Toggle-search Toggle">搜索</li>
<li className="Toggle-toggle Toggle">工具</li>
<li className="Toggle-community Toggle">社区</li>
<li className="Toggle-life Toggle">生活</li>
<li className="Toggle-work Toggle">求职</li>
</ul>
<span className="Toggle-scroll">
<div className="SearchMain-Toggle">
<ul className="Toggle-Top">
<li className="Toggle-usually Toggle" style={{color:"#FFFFFF" }} >常用</li>
<li className="Toggle-search Toggle">搜索</li>
<li className="Toggle-toggle Toggle">工具</li>
<li className="Toggle-community Toggle">社区</li>
<li className="Toggle-life Toggle">生活</li>
<li className="Toggle-work Toggle">求职</li>
</ul>
<span className="Toggle-scroll" style={{left:domLeft}}>
</span>

</div>
<form action="https://www.baidu.com/s?wd=" className="Toggle-From" method="get" target="_blank">
<input type="text" className="Toggle-input" placeholder="百度一下" name="wd"/>
<button type="submit" className="Input-Submit">
<span className="iconfont icon-sousuo Input-sousuo"></span>
</button>
</form>
</div>
);
};
Expand Down
128 changes: 83 additions & 45 deletions src/Components/SearchMain/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,100 @@

.SearchMain{
width: 86vw;
height: px(500);
height: px(650);
position: fixed;
left: px(335);
top: 0;
background-image: url("../../assets/pexels-philippe-donn-1169754.jpg");
background-size: 100% px(400);
display: flex;
align-items: center;
justify-content: center;
.Toggle-Top{
.SearchMain-Toggle{
margin-top: px(250);
display: flex;
.Toggle{
list-style: none;
color: #d1d1d0;
font-size: 18px;
padding-bottom: px(5);
margin: 0 px(30);
user-select: none;
cursor: pointer;
transition: all 0.3s;
position: relative;
}
.Toggle:hover{
color: white;
justify-content: center;
.Toggle-Top{
display: flex;
.Toggle{
list-style: none;
color: #d1d1d0;
font-size: 18px;
padding-bottom: px(5);
margin: 0 px(30);
user-select: none;
cursor: pointer;
transition: all 0.3s;
position: relative;
}
.Toggle:hover{
color: white;
}
.Toggle-usually:after, .Toggle-search:after, .Toggle-toggle:after,
.Toggle-community:after, .Toggle-life:after, .Toggle-work:after{
content: '';
height:px(6);
width: px(6);
border-radius: 50%;
position:absolute;
top: 30px;
left: 50%;
transform: translate(-50%,-50%);
background-color: white;
transition: .3s;
opacity: 0;
}
#active:after{
opacity: 0;
}
.Toggle:hover:after{
opacity: 1;
}
}
.Toggle-usually:after, .Toggle-search:after, .Toggle-toggle:after,
.Toggle-community:after, .Toggle-life:after, .Toggle-work:after{
content: '';
height:px(6);
width: px(6);
border-radius: 50%;
position:absolute;
top: 30px;
left: 50%;
transform: translate(-50%,-50%);
.Toggle-scroll{
display: inline-block;
width: px(50);
height: px(8);
background-color: white;
transition: .3s;
opacity: 0;
border-radius: 5px;
position: absolute;
top: px(295);
transition: all 0.3s;
}
#active:after{
opacity: 0;
}
.Toggle-From{
position: relative;
display: flex;
align-items: center;
width: px(1200);
height: px(70);
left: 50%;
transform: translate(-50%,-50%);
margin-top: px(80);
.Toggle-input{
width: px(1200);
height: px(70);
border: none;
border-radius: 18px;
background-color: #000000;
color: #FFFFFF;
font-size: 18px;
padding-left: px(20);
}
.Toggle:hover:after{
opacity: 1;
.Toggle-input::placeholder{
color: #999999;
}
.Input-Submit{
display: flex;
align-items: center;
.Input-sousuo{
z-index: 100;
color: #FFFFFF;
position: absolute;
right: px(30);
font-size: 24px;
font-weight: 700;
}
}

}
.Toggle-scroll{
display: inline-block;
width: px(50);
height: px(8);
background-color: white;
border-radius: 5px;
position: absolute;
top: px(270);
left: px(680);
transition: all 0.3s;
}


}
80 changes: 80 additions & 0 deletions src/Components/Segmented/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {useEffect, useRef, useState} from 'react';

import './style.scss'

const onMouseOver = (setCurrentIndex) => {
return (e) => {
const index = e.target.getAttribute('index')
if (index !== undefined && index !== null) {
setCurrentIndex(index)
}
}
}
const onMouseLeave = (setCurrentIndex, clickIndex) => {
return () => {
setCurrentIndex(clickIndex)
}
}
const onClick = (setClickIndex) => {
return (e) => {
const index = e.target.getAttribute('index')
if (index !== undefined && index !== null) {
setClickIndex(index)
}
}
}
const obj = [
'热门网址',
'大家喜欢',
'最新网址'
]
const Segmented = () => {
const [currentIndex, setCurrentIndex] = useState(0)
const [refresh, setRefresh] = useState(false)
const [clickIndex, setClickIndex] = useState(0)
const el = useRef(null)
const interval = useRef(null)
const left = useRef(0)
useEffect(() => {
let end = currentIndex * 33;
if (interval.current) {
clearInterval(interval.current)
interval.current = null
}
const eachStep = Math.abs(currentIndex - clickIndex) <= 0 ? 3 : Math.abs(currentIndex - clickIndex) * 2
interval.current = setInterval(() => {
if (left.current < end) {
if (left.current + eachStep > end) {
left.current = end
} else {
left.current += eachStep
}
} else if (left.current > end) {
if (left.current - eachStep < end) {
left.current = end
} else {
left.current -= eachStep
}
} else {
clearInterval(interval.current)
interval.current = null
}
setRefresh((prev) => !prev)
}, 12)
}, [currentIndex, clickIndex])
return (
<div className={'segment'}>
<ul onMouseOver={onMouseOver(setCurrentIndex)} onMouseLeave={onMouseLeave(setCurrentIndex, clickIndex)}
onClick={onClick(setClickIndex)}>
{obj.map((item, index) => {
return (
<li key={index} index={index}
style={index == currentIndex ? {color: 'white'} : {color: 'rgb(136,136,136)'}}>{item}</li>
)
})}
<div className="active" ref={el} style={{left: `${left.current}%`}}></div>
</ul>
</div>
)
}
export default Segmented;
48 changes: 48 additions & 0 deletions src/Components/Segmented/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
$height: 35px;
.segment {
width: calc(100% - 17.44rem);
position: absolute;
left: 20rem;
top: 6rem;
overflow: hidden;
height: $height;
z-index: 1;

ul {
display: flex;
justify-content: center;
list-style: none;
width: 250px;
border-radius: 15px;
border: 1px solid rgb(255, 255, 255);
height: $height;
background-color: rgb(224, 224, 224);
padding: 1px;
position: relative;
z-index: 1;
color: white;

li {
width: 33.33%;
text-align: center;
line-height: $height - 10px;
padding: 3px;
border-radius: 15px;
z-index: 3;
cursor: pointer;
}

.active {
background-color: rgb(67, 63, 77);
position: absolute;
width: 33.33%;
text-align: center;
height: $height - 4px;
left: 0;
padding: 3px;
border-radius: 15px;
color: white;
z-index: 2;
}
}
}
2 changes: 1 addition & 1 deletion src/Components/TopNav/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
position: fixed;
top: 0;
left: px(335);
background-color: rgba(256,256,256,0.75);
background-color: rgba(256,256,256,0.9);
display: flex;
align-items: center;
border-bottom: 1px solid #999999;
Expand Down
Loading

0 comments on commit 8ae5f4a

Please sign in to comment.