使用shake.js讓你部落格支援搖一搖

來源:互聯網
上載者:User

標籤:android   style   blog   color   使用   os   io   for   

大家好,又到了隨機文章的時間,請使用手機開啟示範網站,然後像搖妹子一樣搖晃手機,你會發現非常牛逼的事情,炫酷吧。該功能已經整合在Oconnor1.8中。本文主要講解這貨的原理。首先需要下載shake.js,shake.js github地址,我們只需要裡面的shake.js,然後引入。添加“搖一搖”事件監聽window.addEventListener(‘shake‘, shakeEventDidOccur, false);//function to call when shake occursfunction shakeEventDidOccur () {    //put your own code here etc.    if (confirm("Undo?")) {    }}取消監聽window.removeEventListener(‘shake‘, shakeEventDidOccur, false);隨機文章實現方式JS代碼,當然在這之前需要進入shake.js指令碼window.addEventListener(‘shake‘, shakeEventDidOccur, false);//function to call when shake occursfunction shakeEventDidOccur () {    jQuery.post(Bigfa.ajaxurl, {                action : ‘random_post‘,                }, function(data) {                    window.location.href = data;            });}下面的代碼扔到functions.php中,如果你已經添加了上一篇隨機文章的代碼這裡就不需要wp_enqueue_script( ‘key‘, true);wp_localize_script(‘key‘, ‘Bigfa‘, array(     "ajaxurl" => admin_url(‘admin-ajax.php‘)));add_action( ‘wp_ajax_random_post‘, ‘bigfa_random_post‘ );add_action( ‘wp_ajax_nopriv_random_post‘, ‘bigfa_random_post‘ );function bigfa_random_post() {    $posts = get_posts(‘post_type=post&orderby=rand&numberposts=1‘);    foreach($posts as $post) {        $link = get_permalink($post);    }    echo $link;    exit;}OK。just 搖 it。瀏覽器和裝置支援情況iOS Safari 4.2.1 (and above)Android 4.0.3 (default browser)Opera Mobile (Android)BlackBerry PlayBook 2.0Firefox for AndroidFirefoxOS Devices

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.