选项卡回退历史刷新回到上次点击的选项卡

之前写电商网页的时候,很多个页面的需求是有多个选项卡,点击不同选项卡进行切换,点击当前选项卡的某个链接跳转之后,按回退历史键,如果不进行任何处理,页面会默认回到第一个选项卡,这样客户体验感,之后找到了解决方法,分享出来希望对大家有帮助:

在页面tab切换的时候的,通过history.replaceState将当前的页面状态保持-将修改页面的URL的,但是不会重新加载页面;当页面返回重新显示的时候,判断是否有特定值。

代码如下:

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>History replace</title>
    <script type="text/javascript" src="../js/jquery-1.12.0.js"></script>
    <style>
        .tab{
            width: 300px;
            height: 50px;
            background-color: #0a6ebd;
            display: inline-block;
            border: 1px solid #cccccc;
            font-size: 0px;
            line-height: 50px;
        }

        .tab .tab-option{
            height: 100%;
            width: 100px;
            background-color: #0da6ec;
            color:#000000;
            display: inline-block;
            color:#ffffff;
            font-size: 14px;
            text-align: center;
            vertical-align: middle;
            cursor: pointer;
        }

        .tab .tab-option.selected{
            background-color: #ffffff;
            color:#000000;
        }

        .tab-content{
            width: 100%;
            height: 300px;
            border: 1px solid #cccccc;
            background-color: #ffffff;
            display: none;
        }

        .tab-content.show{
            display: block;
        }

    </style>
    <script>
        $(function(){
            var defaultTabId="";
            $(".tab").on("click",".tab-option",function(){
                var dataTabId=$(this).attr("data-tab-id");
                $(".tab").find(".tab-option").removeClass("selected");
                $(this).addClass("selected");
                $('.tab-content').removeClass("show");
                $('.tab-content[data-tab-id='+dataTabId+']').addClass("show");
                history.replaceState({selectedTab:dataTabId},"","#tab="+dataTabId);
            });
            if(location.hash){
                defaultTabId=location.hash.substr(1).split("=")[1];
                $(".tab").find(".tab-option").removeClass("selected");
                $('.tab-option[data-tab-id='+defaultTabId+']').addClass("selected");
                $('.tab-content').removeClass("show");
                $('.tab-content[data-tab-id='+defaultTabId+']').addClass("show");
            }
        });
    </script>
</head>
<body>
<div class="tab">
    <div class="tab1 tab-option selected" data-tab-id="0">Tab1</div>
    <div class="tab2  tab-option" data-tab-id="1">Tab2</div>
    <div></div>
</div>
<div class="tab-content show" data-tab-id="0">
    <div><a href="divtest.html">detail</a></div>
</div>
<div class="tab-content" data-tab-id="1">
    <div>TAB2 Content
        <div><a href="test.html">detail</a></div>
    </div>
</div>
</body>
</html>

 

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想实现点击选项卡切换加上分类选项卡内容的功能,可以使用uniapp提供的tabBar组件和swiper组件。 首先,你需要在页面中引入tabbar组件和swiper组件: ```html <template> <view> <tabbar :current="current" @change="tabChange"> <tabbar-item title="选项卡1"></tabbar-item> <tabbar-item title="选项卡2"></tabbar-item> <tabbar-item title="选项卡3"></tabbar-item> </tabbar> <swiper :current="current" @change="swiperChange"> <swiper-item> <view>选项卡1对应的内容</view> </swiper-item> <swiper-item> <view>选项卡2对应的内容</view> </swiper-item> <swiper-item> <view>选项卡3对应的内容</view> </swiper-item> </swiper> </view> </template> ``` 在上面的代码中,我们创建了一个tabbar组件和一个swiper组件,分别用来实现选项卡和对应内容的切换。其中,tabbar组件的每个选项卡都有一个title属性,用来显示选项卡的标题。swiper组件的每个swiper-item元素都包含一个view元素,用来显示对应选项卡的内容。 接下来,你需要在页面的<script>标签中添加对应的逻辑代码,用来实现选项卡和内容的切换: ```javascript <script> export default { data() { return { current: 0 } }, methods: { tabChange(e) { this.current = e.index }, swiperChange(e) { this.current = e.current } } } </script> ``` 在上面的代码中,我们定义了一个current变量,用来记录当前选中的选项卡的索引值。然后,我们在tabChange方法和swiperChange方法中分别监听tabbar和swiper的切换事件,通过修改current变量的值来实现选项卡和内容的切换。 这样,你就可以通过uniapp提供的tabbar组件和swiper组件来实现点击选项卡切换加上分类选项卡内容的功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值