react中通过css进行一行四个每点击一次就向右平移一次的动画效果

本文介绍了如何在React应用中使用useState和CSS样式实现一个可平移的教师列表,包括左右按钮控制列表项的移动,以及列表项的显示与隐藏。点击事件触发导航到详细页面。

当前的数组设置为

const [teacherList, setTeacherList] = useState([]);

点击几次的记录 

const [transNum, setTransNum] = useState(0);

向右-1 向左+1

向左向右的两边的按钮按照自己要求设置css,并且到头时按钮隐藏

按钮代码👇

           {teacherList.length > 4 && transNum !== 4 - teacherList.length && (
              <div
                className={styles.bottonRight}
                onClick={() => {
                  if (transNum > 4 - teacherList?.length) {
                    console.log(transNum, gridRef, 'transNum');
                    setTransNum(transNum - 1);
                  }
                }}
              >
                {'>'}
              </div>
            )}
            {teacherList.length > 4 && transNum !== 0 && (
              <div
                className={styles.bottonLeft}
                onClick={() => {
                  if (transNum < 0) {
                    setTransNum(transNum + 1);
                  }
                }}
              >
                {'<'}
              </div>
            )}

平移关键代码,由于每个div间隔为20 所以每次平移为(当前div宽度/4+5)*点击num,挂载当前div获取相关宽度

const gridRef = useRef();

 <div className={styles.grid2Out}>
              <div
                className={styles.grid2}
                ref={gridRef}
                style={{
                  transform: `translateX(${
                    (gridRef?.current?.offsetWidth / 4 + 5) * transNum
                  }px)`,
                  transition: 'all 1s',
                }}
              >
                {teacherList?.map((item, index) => {
                  return (
                    <div
                      key={index}
                      className={styles.teacherListInBox}
                      onClick={() => {
                        navigate('/user/teacherAndLesson', {
                          state: {
                            index: index,
                            id: item?.id,
                          },
                        });
                      }}
                    >
                      <div style={{ height: 182, borderRadius:'10px 10px 0 0',overflow:'hidden'}} className={styles.backImg}>
                        {item.avatar_obj?.[0]?.url ? (
                          <img
                            src={item.avatar_obj?.[0]?.url}
                            alt=""
                            style={{
                              borderRadius: '10px 10px 0 0',
                              width: '100%',
                              height: '100%',
                            }}
                          />
                        ) : (
                          <div
                            style={{
                              backgroundColor: '#f0edf2',
                              width: '100%',
                              height: '100%',
                              borderRadius: 10,
                            }}
                          >
                            <RobotOutlined
                              style={{
                                width: '100%',
                                height: '100%',
                                display: 'flex',
                                justifyContent: 'center',
                                alignItems: 'center',
                              }}
                            />
                          </div>
                        )}
                        {/* <img
                        src={item.avatar_obj?.[0]?.url}
                        alt=""
                        style={{
                          borderRadius: '10px 10px 0 0',
                          width: '100%',
                          height: '100%',
                        }}
                      /> */}
                      </div>
                      <div style={{ padding: 15 }}>
                        <div  className={styles.horverTitle} style={{ marginBottom: 
                             15 }}>{item.name}</div>
                        <div style={{ fontSize: 12, color: '#aaa' }}>
                          {item.position}
                        </div>
                      </div>
                    </div>
                  );
                })}
              </div>
            </div>

css文件👇

.grid2Out{
    overflow: hidden;
}
.grid2{
    display: flex;
    flex: 1;
    flex-wrap: nowrap;
    justify-content: flex-start;
    // overflow:hidden;
    .teacherListInBox{
        img{
            transform: scale(1);
            transition: all 1s,
          }
    cursor: pointer;
      flex: 1;
      height: 260px;
      box-shadow: 0 10px 20px rgba(0,0,0,.02);
      margin: 20px 20px 20px 0;
      box-sizing: border-box;
    //   border: 1px solid #e3e0e0;
      border-radius: 10px;
      width: calc((100% - 60px) / 4);
      min-width: calc((100% - 60px) / 4);
      max-width: calc((100% - 60px) / 4);
    //   &:nth-child(4n + 4) {
    //     margin-right: 0;
    //   }  
    }
    .horverTitle{
        font-size: 16px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }
   .teacherListInBox:hover{
    box-shadow: 0 10px 20px rgba(1, 1,1, 0.05);
    img{
      transform: scale(1.1);
      transition: all 1s,
    }
    .horverTitle{
        color:#1677ff;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
   }
}
.bottonRight{
    position: absolute;
    right: -40px;
    top: 50%;
    width: 30px;
    height: 30px;
    border-radius: 15px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 2px solid  #aaaaaa42;
    box-shadow: #aaa;
    cursor: pointer;
}
.bottonLeft{
    position: absolute;
    left: -40px;
    top: 50%;
    width: 30px;
    height: 30px;
    border-radius: 15px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 2px solid  #aaaaaa42;
    cursor: pointer;

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值