Using jQuery and PHP to achieve a similar 360 function switch effect

  • 2020-03-30 01:39:40
  • OfStack

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / 201411292724 ">

In order to better demonstrate this example, we need a data table to record the required function description and open status. The table structure is as follows:

CREATE TABLE `pro` (  
  `id` int(11) NOT NULL auto_increment,  
  `title` varchar(50) NOT NULL,  
  `description` varchar(200) NOT NULL,  
  `status` tinyint(1) NOT NULL default '0',  
  PRIMARY KEY  (`id`)  

You can insert several pieces of data into the pro table.


We will display a list of related functions on the page, read the data table using PHP, and present it as a list.

   require_once('connect.php'); //Connect to database & NBSP;  
   $query=mysql_query("select * from pro order by id asc");   
   while ($row=mysql_fetch_array($query)) {   
   <div class="list">   
     <div class="fun_title">   
        <span rel="<?php echo $row['id'];?>" <?php if($row['status']==1){ ?>   
class="ad_on" title=" Click on close "<?php }else{?>class="ad_off" title=" Click to open "<?php }?>></span>   
        <h3><?php echo $row['title']; ?></h3>   
     <p><?php echo $row['description'];?></p>   
 <?php } ?>

Connect to the database and loop out the list of product features.


In order to render a better page appearance, we use CSS to beautify the page and make the page more humanized. Using CSS, we only need a picture to identify the switch buttons.

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / 201411292743 ">

.list{padding:6px 4px; border-bottom:1px dotted #d3d3d3; position:relative}   
.fun_title{height:28px; line-height:28px}   
.fun_title span{width:82px; height:25px; background:url(switch.gif) no-repeat;    
cursor:pointer; position:absolute; right:6px; top:16px}   
.fun_title span.ad_on{background-position:0 -2px}   
.fun_title span.ad_off{background-position:0 -38px}   
.fun_title h3{font-size:14px; font-family:'microsoft yahei';}   
.list p{line-height:20px}   
.list p span{color:#f60}   

The CSS code, I don't want to go into details, but let me remind you that we use a picture, and then we use background-position to locate the picture, which is what most websites do, so I won't say anything about that.


We click the switch button, timely request the background, change the corresponding function of the switch state. This process is a typical Ajax application. By clicking the switch button, the front end sends a post request to the background PHP, the background receives the request, queries the database, and returns the result to the front end. The front end jQuery changes the state of the button according to the result returned from the background.

    //Sliding the mouse towards the color change & NBSP;  

        var add_on = $(this);   
        var status_id = $(this).attr("rel");   
                add_on.removeClass("ad_on").addClass("ad_off").attr("title"," Click to open ");   
        var add_off = $(this);   
        var status_id = $(this).attr("rel");   
                add_off.removeClass("ad_off").addClass("ad_on").attr("title"," Click on close ");   

Note: in the code, we first realized the function of changing colors by mouse sliding to the function list (see the demo), and then we clicked the switch button to send an Ajax request to the background action.php. The parameters submitted are the id and type of the corresponding function, which is used to distinguish the function and the type of the request (open and close) in the background. In fact, if you're a little careful, you can see that the switch button dynamically changes its style after a successful return based on the Ajax request, enabling it to change its on-off state.

Action. PHP

The background action. PHP receives the request from the front end, executes the SQL statement according to the parameters, updates the status of the corresponding function, and returns the result to the front end after success, please see the code:

$id = $_POST['status'];   
$type = $_POST['type'];   
if($type==1){ //Close    
    $sql = "update pro set status=0 where id=".$id;   
}else{ //Open    
    $sql = "update pro set status=1 where id=".$id;   
$rs = mysql_query($sql);   
    echo '1';   
    echo ' The server is busy, please try again later! ';   

Related articles: