DOM侧边栏

JavaScript DOM侧边栏

1.首先先建一个盒子设置一个div,div里面加个span,给这个div设置样式: *{padding: 0;margin: 0;}
.box{width: 200px;height: 250px;background: skyblue;position: relative;
margin-top: 400px;left: -200px;}
.box span{position: absolute;left: 200px;background: red;top: 50%;
display: inline-block;width: 25px;text-align: center;transform: translateY(-50%)};
设置过样式之后就该写鼠标事件如下代码:

 var o=document.querySelector('.box');
        var t;
        //鼠标经过事件
        o.onmouseenter=function(){   
            clearInterval(t);         
            t=setInterval(move,20);
        };
        //鼠标离开事件
        o.onmouseleave=function(){
            clearInterval(t);
            t=setInterval(moveLeft,20);
        };
        function move(){
            // console.log('move');
            var l=getComputedStyle(o,null).left;//获取左边距
            l=parseFloat(l);
        //    console.log('r',l);
            //若移动到边缘是,停止移动
            if(l>=0){
                clearInterval(t);
            }else{
                o.style.left=l+5+'px';//向右移动
            }            
        }
        function moveLeft(){
            var l=getComputedStyle(o,null).left;//获取左边距
            l=parseFloat(l);
             //若移动到边缘是,停止移动
             if(l<=-200){
                clearInterval(t);
            }else{
                o.style.left=l-5+'px';//向右移动
            } 
        }

这就跟侧边广告弹窗差不多,里面加些样式就可以。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要设置侧边栏路由,你可以使用 React Router 和一个侧边栏组件来实现。以下是一个简单的示例: 1. 首先,在你的应用程序中安装并导入 React Router: ``` npm install react-router-dom ``` ``` import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; ``` 2. 创建一个侧边栏组件,它将包含你的侧边栏链接: ``` import { Link } from 'react-router-dom'; function Sidebar() { return ( <div> <ul> <li><Link to="/">Home</Link></li> <li><Link to="/about">About</Link></li> <li><Link to="/contact">Contact</Link></li> </ul> </div> ); } ``` 3. 创建你的路由组件,并将其包裹在 Router 组件中。在你的路由中,将你的组件与对应的路径匹配: ``` function App() { return ( <Router> <div> <Sidebar /> <Switch> <Route exact path="/" component={Home} /> <Route path="/about" component={About} /> <Route path="/contact" component={Contact} /> </Switch> </div> </Router> ); } ``` 在这个例子中,Home、About、Contact 是三个组件,它们将分别与路径 /、/about 和 /contact 匹配。 4. 最后,在你的组件中,你可以使用 React Router 提供的 useHistory 和 useLocation 钩子来检测当前路径,并在侧边栏中高亮对应的链接: ``` import { useHistory, useLocation } from 'react-router-dom'; function Sidebar() { const history = useHistory(); const location = useLocation(); const isActive = (pathname) => { return location.pathname === pathname ? 'active' : ''; } return ( <div> <ul> <li className={isActive('/')}><Link to="/">Home</Link></li> <li className={isActive('/about')}><Link to="/about">About</Link></li> <li className={isActive('/contact')}><Link to="/contact">Contact</Link></li> </ul> </div> ); } ``` 这个例子中,isActive 函数将检查当前路径是否与给定的路径匹配,如果匹配,则为该链接添加一个名为 active 的类。 这是一个简单的侧边栏路由设置的示例,你可以根据你的应用程序需要进行更改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值