JQuery Mobile External Tool Bar ui-btn-active样式问题

在使用JQuery Mobile时我需要使用外部的工具栏,并使用了ui-btn-active样式来设定活动标签的样式,发现在外部固定工具栏状态下,ui-btn-active在生效后马上实效,经调试发现时JQM库的问题(我提交了Github issue),通网上找了一个示例,他通过JS解决了这个问题,我用他的方法也不行,他用的版本不是1.4.5. 

目前有两个解决方案:

  1. 通过修改JQM的js代码

for JQM1.4.5

jquery.mobile-1.4.5.js#420

removeActiveLinkClass: function( forceRemoval ) {
			if ( !!$.mobile.activeClickedLink &&
				( !$.mobile.activeClickedLink.closest( "." + $.mobile.activePageClass ).length ||
					forceRemoval ) ) {

				//<--$.mobile.activeClickedLink.removeClass( $.mobile.activeBtnClass );
			}
			$.mobile.activeClickedLink = null;
		},
jquery.mobile-1.4.5.js#7434

// The code below is a workaround to fix #1181

$( document ).one( "pagehide", function() {

//<--activeBtn.removeClass( $.mobile.activeBtnClass );

});



  1. 通过自定义样式,通过脚本进行设定(推荐)

2.1 在标签中添加对自定义样式的支持,比如:my-btn-active样式,对page添加page-title属性,该属性值为导航条的文本值

页面:

<div data-role="page" id="vbook-personal" data-title="个人">

</div>

<div data-role="page" id="vbook-store" data-title="教材库">

</div>

导航条标签:

<li id="personal"><a href="#vbook-personal" data-theme="d" class="ui-state-persist">个人</a></li>

<li id="bookshelf"><a href="#vbook-bookshelf"  data-theme="d" class="ui-btn-active ui-state-persist">我的教材</a></li>

<li id="library"><a href="#vbook-store" data-theme="d" class="ui-state-persist">教材库</a></li>


2.2 定义有my-btn-active和没有my-btn-active两种样式

/*主页-个人菜单活动状态下背景和颜色*/

div.ui-footer .ui-navbar ul li#personal a.my-btn-active {

    background: url(../images/ico-person-active.png) 50% 20% no-repeat;

    background-size:25px;

    color:#ffffff;

}

/*主页-个人菜单正常状态下背景和颜色*/

div.ui-footer .ui-navbar ul li#personal a {

    background: url(../images/ico-person.png) 50% 20% no-repeat;

    background-size:24px;

    color:#cccccc;

}


2.3 在页面添加响应的脚本实现样式添加和删除

// Update the contents of the toolbars
$(document).on("pageshow", "[data-role='page']", function () {
    // Each of the four pages in this demo has a data-title attribute
    // which value is equal to the text of the nav button
    // For example, on first page: <div data-role="page" data-title="Info">
    var current = $(this).jqmData("title");
    // Change the heading
    //$("[data-role='header'] h1").text(current);
    // Remove active class from nav buttons
    $("[data-role='navbar'] a.my-btn-active").removeClass("my-btn-active");
    // Add active class to current nav button
    $("[data-role='navbar'] a").each(function () {
        if ($(this).text() === current) {
            $(this).addClass("my-btn-active");
        }
    }); 
});

参考:

http://stackoverflow.com/questions/19574429/jqm-1-4-beta-persistent-navbar-not-keeping-active-state-class-for-links


版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/yin138/p/4902239.html

Vuex是一个专为Vue.js应用程序设计的状态管理模式,它允许你在整个应用中集中管理数据状态,使得组件之间的通信变得简单。如果你想通过Vuex来控制`vxe-pager`组件中的`num-btn`的数量按钮的`is-active`样式,通常会涉及以下几个步骤: 1. **创建或更新 Vuex store**: 在Vuex store中,定义一个状态(state)来表示当前页码或者按钮的状态。例如,你可以有一个名为`activeBtnIndex`的变量来存储当前活跃按钮的索引。 ```javascript state: { activeBtnIndex: null }, ``` 2. **添加 getters**: 创建一个getter来获取这个状态,以便其他模块可以方便地访问。 ```javascript getters: { getActiveBtnIndex: state => state.activeBtnIndex } ``` 3. **定义 actions**: 当需要修改`is-active`样式时,创建一个action。比如用户点击了某个按钮,你可以触发这个action并更新`activeBtnIndex`。 ```javascript mutations: { setActiveBtnIndex(index) { this.activeBtnIndex = index; } }, actions: { setActiveBtn({ commit }, index) { commit('setActiveBtnIndex', index); } }, ``` 4. **在组件中监听状态变化**: 在使用`vxe-pager`的组件内,你需要订阅store中的`setActiveBtnIndex` mutation,并在接收到新的值时改变按钮的`is-active`样式。 ```vue <template> <vxe-pager :num-btns="numBtns" @update-page="handlePageChange"></vxe-pager> </template> <script> import { mapActions } from 'vuex'; export default { // ... computed: { ...mapGetters(['getActiveBtnIndex']), numBtns() { // 根据实际需求计算按钮数量 return [1, 2, 3]; // 示例 } }, methods: { handlePageChange(index) { this.$store.dispatch('setActiveBtn', index); // 更新按钮样式,假设每个按钮有ref this.$refs.numBtns.forEach((btn, i) => { btn.is_active = i === this.getActiveBtnIndex ? true : false; }); } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值