


How to use jQuery and HTML5 to achieve the special effect of changing clothes by shaking your phone
We often use WeChat to find people nearby, which is achieved by shaking the phone on the app. Now, we have moved this technology to the mobile web for everyone to learn. Friends in need can refer to it.
Shaking your phone can be used in many scenarios, such as shaking to change lottery draws, shaking to search for songs, etc. In this article, I will introduce to you how to use HTML5 PHP jQuery to achieve the effect of changing clothes by shaking your mobile phone.
Note that this is an article about the comprehensive application of WEB knowledge. The prerequisite for reading this article is that you need to have basic knowledge of HTML5, jQuery, PHP, MySQL and other related aspects.
HTML
My page displays product information by default (a certain brand of dress product pictures and text descriptions). Of course, in actual applications, it can be obtained from the database Get product information.
<p id="pro" rel="1"> <p>使劲晃动您的手机</p> <img src="images/z1.jpg" width="300" height="300"> <p>灰色</p> </p>
Then load the jQuery library file in the page, and we continue to use the previous article: "Using HTML5 to implement the function of shaking the phone The code used in the tutorial to listen to the shaking of the phone: shake.js.
<script src="jquery.js"></script> <script src="shake.js"></script>
jQuery
We use shake.js to detect that the user’s phone is shaking. When shaking occurs, the function shakeEventDidOccur() is called to send an Ajax request to the background product.php. The background program will return the corresponding JSON data based on the submitted request parameters. We display the corresponding product pictures and text information based on the returned data to achieve the effect of changing clothes.
window.onload = function() { var myShakeEvent = new Shake({ threshold: 15 }); myShakeEvent.start(); window.addEventListener('shake', shakeEventDidOccur, false); function shakeEventDidOccur () { var pro_id = $("#pro").attr("rel"); $.getJSON("product.php?id="+pro_id,function(json){ if(json.msg==1){ $("#pro").attr("rel",json.pro.id) .html('<img src="images/'+json.pro.pic+'" width="300" height="300"><p>'+json.pro.color+'</p>'); }else{ alert(json.msg); } }); } };
PHP
Backend product.php receives the parameter id submitted by ajax , query the data information other than the current ID in the database, obtain the corresponding data set results, and then randomly take out a set of data from the data set (because only one piece of data information is displayed at a time), and return it to the front-end call in JSON format, please see Code:
<?php //连接数据库 include_once("connect.php"); $id = intval($_GET['id']); if($id==0) exit; //查询数据 $query = mysql_query("select * from dress where id!='$id'"); $total = mysql_num_rows($query); $arr = array(); if($total==0){ $arr['msg'] = '没有足够的衣服!'; }else{ $arr['msg'] = 1; while($row=mysql_fetch_array($query)){ $pros[] = array( 'id' => $row['id'], 'color' => $row['color'], 'pic' => $row['pic'] ); } //随机取一组数据 $arr['pro'] = $pros[array_rand($pros)]; } //输出JSON格式数据 echo json_encode($arr); ?>
Of course, this article is just an example application. During development, you can optimize the PHP program code according to the actual application to create high-quality PHP that suits your project. Code, and finally the mysql data table structure:
CREATE TABLE IF NOT EXISTS `dress` ( `id` int(11) NOT NULL AUTO_INCREMENT, `color` varchar(30) NOT NULL, `pic` varchar(30) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ; INSERT INTO `dress` (`id`, `color`, `pic`) VALUES (1, '灰色', 'z1.jpg'), (2, '紫色', 'z2.jpg'), (3, '红色', 'z3.jpg'), (4, '蓝色', 'z4.jpg');
The above is the entire content of this article, I hope it will be helpful to everyone's learning, more Please pay attention to the PHP Chinese website for related content!
Related recommendations:
html5 implements parsing of players compatible with major browsers
HTML discount calculation price Implementation principle and script code
The above is the detailed content of How to use jQuery and HTML5 to achieve the special effect of changing clothes by shaking your phone. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
