Home > php教程 > PHP源码 > body text

ThinkPHP3.2.3实现手机摇一摇随机推荐效果

PHP中文网
Release: 2016-05-23 16:39:02
Original
1468 people have browsed it

手机摇一摇效果基于html5方向感应DeviceOrientation,它将底层的方向和运动传感器进行了高级封装,轻松的实现重力感应、指南针等有趣的功能。今天我们以实例展示THINKPHP3.2.3实现手机摇一摇随机推荐文章效果。可手机访问www.gouugoyin.cn首页,用手机摇一摇,即可在右侧看到摇一摇随机推荐效果。

[PHP]代码 

首先在需要摇一摇的模板页引入jquery.js和检测手机摇晃的shade.js,HTML结构如下

<script src="{JS_PATH}/jquery-1.7.1.min.js?1.1.5"></script>
<script src="{JS_PATH}/shake.js?1.1.5"></script>
<p class="sidebar-widget js_shadeBox">
    <h4>
       摇一摇随机推荐
    </h4>
    <ul>
 
    </ul>
</p>
使用shake.js检测到用户手机摇晃,当摇晃发生时调用函数shakeEventDidOccur(),向服务器发送Ajax请求,JS代码如下

<script>
$(function(){
	var myShakeEvent = new Shake({  
        threshold: 15  
    });  
  
    myShakeEvent.start();  
  
    window.addEventListener(&#39;shake&#39;, shakeEventDidOccur, false);  
  
    function shakeEventDidOccur () { 
    	var url = "{:U(&#39;home/article/get_rand_list&#39;)}";
    	$.post(url , { num:10,order:&#39;rand()&#39; }, function(data){
    		if(data.status == &#39;ok&#39;){
    			$(".js_shadeBox ul").append(data.html);
    		}else{
    			alert(data.msg);
    		}
    	}, &#39;json&#39;).error(function(){
    		alert(&#39;网络错误,请稍后再试&#39;);
    	});
    } 
});
</script>
服务器根据提交的请求参数生成随机文章列表数组并循环拼接好的html代码片段,我们将返回html片段追加到ul里,实现了摇一摇随机推荐的效果,服务器端代码如下


<?php
/**
 * 文章控制器 ArticleController.class.php
 * ============================================================================
 * 版权所有 (C) 2015-2016 够过瘾——挨踢男的葵花宝典。
 * 网站地址:   http://www.gouguoyin.cn
 * ----------------------------------------------------------------------------
 * 许可声明:这是一个开源程序,未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * Author: 勾国印 (phper@gouguoyin.cn) 
 * Date: 2015年9月27日 上午11:27:53  
*/
namespace Home\Controller;
use Think\Controller;

class ArticleController extends Controller {

    //ajax获取随机文章列表
    public function get_rand_list(){
        $model = &#39;article&#39;;
        if (!IS_AJAX) {
            $this->ajaxReturn(array(
                &#39;msg&#39; => &#39;非法登录方式&#39;
            ));
        }
        
        //接收参数
        $order       = I(&#39;order&#39;, &#39;id desc&#39;);
        $num         = I(&#39;num&#39;, 0);
        
        $filter = array();
        
        if ($category_id) {
            $filter[&#39;category_id&#39;] = $category_id;
        }
        
        $article_list = M($model)->where($filter)->limit($num)->order($order)->select();
        
        foreach ($rand_list as $k => $v) {
            $html .= "<li><a target=&#39;_blank&#39; href=&#39;".U("home/article/$v[id]")."&#39;>".$v[&#39;title&#39;]."</li>";
        }
        $this->ajaxReturn(array(
            &#39;status&#39; => &#39;ok&#39;,
            &#39;html&#39; => $html
        ));
        
    }

}备注:ThinkPHP3.2.3控制器里获取随机列表的方法是:M($model)->where($where)->order(&#39;rand()&#39;)->limit($limit)->select();
Copy after login


2. [文件] THINKPHP3.2.3实现手机摇一摇随机推荐文章效果

ThinkPHP3.2.3实现手机摇一摇随机推荐效果THINKPHP3.2.3实现手机摇一摇随机推荐文章效果.zip

       



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template