Home  >  Article  >  Web Front-end  >  jquery php implements scrolling digital effects_jquery

jquery php implements scrolling digital effects_jquery

WBOY
WBOYOriginal
2016-05-16 15:29:092549browse

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 real-time acquisition of the download count of a product as a scenario. The frontend executes javascript regularly to obtain the latest download count, and updates the download count on the page on a rolling basis.
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> 

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

<div id="total"> 
  下载量:<span class="t_num"></span>次 
</div> 

jQuery
First, let's 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 5a8028ccc7a7e27417bff9f05adf593272ac96585ae54b6ae11f849d2649d9e6, 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(){} 
    ); 
  } 
} 

Next, we get the latest download count in the background through ajax. The following code is a common jQuery ajax request. It makes a post request to data.php, data.php or obtains the latest number of downloads. After successful processing, the number of downloads is obtained: data.count, and then calls show_num() to implement digital scrolling. .

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

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

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

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

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Statement:
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