Stay Hungry,Stay Foolish!

partial.js client-side routing(客户端路由-基于HTML5 SPA特性的历史API)

partial.js client-side routing

https://github.com/petersirka/partial.js-clientside

Framework supports the HTML 5 History API, for older browsers (IE8+) URL hashtag is automatically enabled. This plugin is a little big cannon for the web development. Works best with jQuery.

  • easy to use
  • minified only 9.5 kB (without GZIP compression)
  • great functionality
  • great use
  • works in IE 8+
  • no dependencies
  • DEMO EXAMPLE

 

小巧 易用 功能强大, 兼容性好, 对于老的浏览器, 即不支持HTML5 SPA 历史API的浏览器, 自动降级为 hash方式的路由。

查看例子页面:

http://source.858project.com/contact/

Simple example

// ===========================
// DEFINE ROUTING
// ===========================

// framework === global variable

framework.route('/homepage/', view_homepage, ['contact']);
framework.route('/services/', view_services, ['contact']);
framework.route('/contact/', view_contact, ['empty']);
framework.route('/products/{category}/', view_products, ['latest']);

// ===========================
// DEFINE PARTIAL CONTENT
// ===========================

framework.partial('contact', function() {
    $('#panel').html('PANEL CONTACT');
});

framework.partial('empty', function() {
    $('#panel').html('PANEL EMPTY');
});

framework.partial('latest', function() {
    $('#panel').html('PANEL LATEST');
});

// ===========================
// DEFINE VIEWS
// ===========================

function view_homepage() {
    $('#content').html('HOMEPAGE');
}

function view_services() {
    $('#content').html('SERVICES');
}

function view_contact() {
    $('#content').html('CONTACT');
}

function view_products(category) {
	$('#content').html('PRODUCTS –> ' + category);
}

// ===========================
// DEFINE EVENTS
// ===========================

framework.on('ready', function() {
    $('.menu').on('click', 'a', function(e) {
        e.preventDefault();
        e.stopPropagation();
        framework.redirect($(this).attr('href'));
    });
    framework.redirect('/homepage/');
});

framework.on('location', function(url) {
     var menu = $('.menu');
     menu.find('.selected').removeClass('selected');
     menu.find('a[href="' + url + '"]').parent().addClass('selected');
});

 

posted @   lightsong  阅读(258)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2014-09-27 JQuery html API支持解析执行Javascript脚本功能实现-代码分析
千山鸟飞绝,万径人踪灭
点击右上角即可分享
微信分享提示