手机摇一摇效果基于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('shake', shakeEventDidOccur, false); function shakeEventDidOccur () { var url = "{:U('home/article/get_rand_list')}"; $.post(url , { num:10,order:'rand()' }, function(data){ if(data.status == 'ok'){ $(".js_shadeBox ul").append(data.html); }else{ alert(data.msg); } }, 'json').error(function(){ alert('网络错误,请稍后再试'); }); } }); </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 = 'article'; if (!IS_AJAX) { $this->ajaxReturn(array( 'msg' => '非法登录方式' )); } //接收参数 $order = I('order', 'id desc'); $num = I('num', 0); $filter = array(); if ($category_id) { $filter['category_id'] = $category_id; } $article_list = M($model)->where($filter)->limit($num)->order($order)->select(); foreach ($rand_list as $k => $v) { $html .= "<li><a target='_blank' href='".U("home/article/$v[id]")."'>".$v['title']."</li>"; } $this->ajaxReturn(array( 'status' => 'ok', 'html' => $html )); } }备注:ThinkPHP3.2.3控制器里获取随机列表的方法是:M($model)->where($where)->order('rand()')->limit($limit)->select();
2. [文件] THINKPHP3.2.3实现手机摇一摇随机推荐文章效果
THINKPHP3.2.3实现手机摇一摇随机推荐文章效果.zip

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
