Home > Web Front-end > JS Tutorial > jquery+php realizes dynamic digital display effect

jquery+php realizes dynamic digital display effect

php中世界最好的语言
Release: 2018-04-24 11:24:03
Original
2005 people have browsed it

This time I will bring you jquery php to achieve dynamic digital display effect. What are the precautions for jquery php to achieve dynamic digital display effect. The following is a practical case, let’s take a look.

Sometimes we need to dynamically display the number of visits, downloads and other effects. We can use jQuery combined with background php to achieve a rolling digital display effect.

This article uses the scenario of obtaining the download count of a product in real time. The front desk executes javascript regularly to obtain the latest download count, and scrolls to update the downloads on the page. frequency.
HTML
We first load the jQuery library file and animated background plug-in: animateBackground-plugin.js.

<script type="text/javascript" src="js/jquery.js"></script> 
<script type="text/javascript" src="js/animateBackground-plugin.js"></script>
Copy after login

Then we add the html element to show the digital scrolling effect in the appropriate position on the page.

<p id="total"> 
  下载量:<span class="t_num"></span>次 
</p>
Copy after login

jQuery
First write a function show_num(), which is used to implement dynamic rolling numbers. We split the statistical number n into individual numbers. These numbers are surrounded by , and the image is positioned on each corresponding number by calling the plug-in backgroundPosition.

function show_num(n){ 
  var it = $(".t_num i"); 
  var len = String(n).length; 
  for(var i=0;i<len;i++){ 
    if(it.length<=i){ 
      $(".t_num").append("<i></i>"); 
    } 
    var num=String(n).charAt(i); 
    var y = -parseInt(num)*30; //y轴位置 
    var obj = $(".t_num i").eq(i); 
    obj.animate({ //滚动动画 
      backgroundPosition :'(0 '+String(y)+'px)' 
      }, 'slow','swing',function(){} 
    ); 
  } 
}
Copy after login

Next, we get the latest number of downloads in the background through ajax. The following code is a common jQuery ajax request. Through post request to data.php, data.php or get the latest number of downloads. After successful processing, the number of downloads will be obtained: data.count, and then call show_num() implements numerical scrolling.

function getdata(){ 
  $.ajax({ 
    url: 'data.php', 
    type: 'POST', 
    dataType: "json", 
    cache: false, 
    timeout: 10000, 
    error: function(){}, 
    success: function(data){ 
      show_num(data.count); 
    } 
    }); 
}
Copy after login

Finally, we need to initialize the data after the page is loaded, and then execute an ajax request every 3 seconds to update the number of downloads:

$(function(){ 
  getdata(); 
  setInterval('getdata()', 3000);//每隔3秒执行一次 
});
Copy after login

Similarly, you can count website visits and statistics Video playback times, countdowns, etc. are used. As for how the background data.php processes data, it is beyond the scope of this article. Interested students can write a background program such as a counter to return data.count.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

jQuery animation effect picture carousel implementation (with code)

jQuery picture carousel slide show Film effect

The above is the detailed content of jquery+php realizes dynamic digital display effect. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template