隨著頁面滾動,數字自動增大的jquery特效

来源:https://www.cnblogs.com/chenyingying0/archive/2020/02/25/12361767.html
-Advertisement-
Play Games

首先為了截出gif圖,我下載了一個小工具 GifCam: https://www.appinn.com/gifcam/ 隨著頁面滾動,數字自動增大的jquery特效 主要就是依賴這個腳本script.js // JavaScript Document $.fn.countTo = function ...


首先為了截出gif圖,我下載了一個小工具

GifCam: https://www.appinn.com/gifcam/

 

隨著頁面滾動,數字自動增大的jquery特效

 

主要就是依賴這個腳本script.js

// JavaScript Document
$.fn.countTo = function (options) {
        options = options || {};
        
        return $(this).each(function () {
            // set options for current element
            var settings = $.extend({}, $.fn.countTo.defaults, {
                from:            $(this).data('from'),
                to:              $(this).data('to'),
                speed:           $(this).data('speed'),
                refreshInterval: $(this).data('refresh-interval'),
                decimals:        $(this).data('decimals')
            }, options);
            
            // how many times to update the value, and how much to increment the value on each update
            var loops = Math.ceil(settings.speed / settings.refreshInterval),
                increment = (settings.to - settings.from) / loops;
            
            // references & variables that will change with each update
            var self = this,
                $self = $(this),
                loopCount = 0,
                value = settings.from,
                data = $self.data('countTo') || {};
            
            $self.data('countTo', data);
            
            // if an existing interval can be found, clear it first
            if (data.interval) {
                clearInterval(data.interval);
            }
            data.interval = setInterval(updateTimer, settings.refreshInterval);
            
            // initialize the element with the starting value
            render(value);
            
            function updateTimer() {
                value += increment;
                loopCount++;
                
                render(value);
                
                if (typeof(settings.onUpdate) == 'function') {
                    settings.onUpdate.call(self, value);
                }
                
                if (loopCount >= loops) {
                    // remove the interval
                    $self.removeData('countTo');
                    clearInterval(data.interval);
                    value = settings.to;
                    
                    if (typeof(settings.onComplete) == 'function') {
                        settings.onComplete.call(self, value);
                    }
                }
            }
            
            function render(value) {
                var formattedValue = settings.formatter.call(self, value, settings);
                $self.html(formattedValue);
            }
        });
    };
    
    $.fn.countTo.defaults = {
        from: 0,               // the number the element should start at
        to: 0,                 // the number the element should end at
        speed: 1000,           // how long it should take to count between the target numbers
        refreshInterval: 100,  // how often the element should be updated
        decimals: 0,           // the number of decimal places to show
        formatter: formatter,  // handler for formatting the value before rendering
        onUpdate: null,        // callback method for every time the element is updated
        onComplete: null       // callback method for when the element finishes updating
    };
    
    function formatter(value, settings) {
        return value.toFixed(settings.decimals);
    }



  // custom formatting example
  $('#count-number').data('countToOptions', {
    formatter: function (value, options) {
      return value.toFixed(options.decimals).replace(/\B(?=(?:\d{3})+(?!\d))/g, ',');
    }
  });
  
function count1(options) {
 document.getElementById('dt').style.display = "block"; //鎺у埗鐩掑瓙鏄劇ず
}
 
  function count(options) {
    var $this = $(this);
    options = $.extend({}, options || {}, $this.data('countToOptions') || {});
    $this.countTo(options);
  }

 

使用時首先在頁面引入jquery.js和script.js

給要動畫的元素添加 timer 類,data-to 表示最終到達的數字, data-speed 表示動畫速度

<h2 class="timer" data-to="1000" data-speed="4000">1000</h2>

 

調用時的腳本

<script type="text/javascript">
    $('.timer').each(count) 
    $('.timer').each(count1);
</script>

 

接下來是完整的頁面

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</head>

<body>

<h2 class="timer" data-to="1000" data-speed="4000">1000</h2>

<script type="text/javascript">
    $('.timer').each(count) 
    $('.timer').each(count1);
</script>

</body>
</html>

 

當然我這個比較醜,找了個好看的demo 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<style>
@charset "utf-8";
/* CSS Document */
/*時間人數開始*/
.time{ width:100%; height:298px; float:left; background:url(images/timebg.png) no-repeat center; background-size:cover;}
.time_content{ width:1200px; height:298px; margin:0 auto;}
.time_left{ width:40%; max-width:480px; float:left; height:298px;}
.time_left h1{ width:100%; float:left; height:24px; line-height:24px; font-size:22px; text-align:center; color:#FFF; font-weight:400; margin-top:96px;}
.time_left h2{ width:100%; float:left; height:56px; line-height:56px; font-size:54px; text-align:center; color:#FFF; margin-top:12px; font-weight:bold;}

.time_right{ width:60%; max-width:720px; height:298px; float:left;}
.wrapper { width: 630px; height:90px; float:left; margin-left:100px; margin-top:110px; display:none;}
.wrapper1{ width:200px; float:left; height:90px;}
.wrapper2{ width:200px; float:left; height:90px; margin-left:20px; border-right:1px solid #FFF; border-left:1px solid #FFF;}
.wrapper3{ width:200px; float:left; height:90px;}

.counter { color:#ffe400; border-radius: 5px;}
.time1{ width:100px; height:54px; float:left;}
.time1 h1{ float:left;}
.time1 h2{ float:left;}
.time2{ width:70px; height:54px; float:left; margin-left:50px;}
.time3{ width:100px; height:54px; float:left; text-align:center;margin-left:40px;}
.timex{ width:50px; height:30px; margin-top:24px;color:#FFF; float:left;}
.timey{ width:200px; float:left; color:#FFF;}
.wrapper2 .timey{ width:148px; margin-left:52px; float:left;}
.wrapper3 .timey{ width:152px; margin-left:48px; float:left;}
.count-title { font-weight: normal; text-align: center; }
.count-text { font-size: 13px; font-weight: normal;  margin-top: 10px; margin-bottom: 0; text-align: center; }
/*時間人數結束*/
</style>
</head>

<body>

<div class="time">
    <div class="time_content">
        <div class="time_left">
            <h1>真實數據&nbsp;放心選擇</h1>
            <h2>專註學歷教育15年</h2>
        </div>
        <div class="time_right">
            <div class="wrapper" id="dt">
            
                <div class="wrapper1">
                    <div class="counter col_fourth time1">
                      <h2 class="timer count-title" data-to="300000" data-speed="2000">300000</h2>
                    </div>
                    <div class="timex">以上</div>
                    <div class="timey">付費學員</div>
                 </div>
                 
                 <div class="wrapper2">
                    <div class="counter col_fourth time2">
                      <h2 class="timer count-title" data-to="107" data-speed="2000">107</h2>
                    </div>
                    <div class="timex"></div>
                    <div class="timey">地區分部</div>
                 </div>
                 
                 <div class="wrapper3">
                    <div class="counter col_fourth time3">
                      <h2 class="timer count-title" data-to="1000" data-speed="2000">1000</h2>
                    </div>
                    <div class="timex">多人</div>
                    <div class="timey">教研團隊</div>
                 </div>
            </div>
            
            <script type="text/javascript">
                 $('.timer').each(count) 
                 $('.timer').each(count1);
            </script>
            
        </div>
    </div>
</div>

<script type="text/javascript">
    $('.timer').each(count) 
    $('.timer').each(count1);
</script>

</body>
</html>

效果圖


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 首先曬出封裝好的dropdown.js (function($){ 'use strict';//使用嚴格模式 //構造函數形式 function Dropdown(elem,options){ //保存到this中才能公用 this.elem=elem; this.options=options; ...
  • 自己封裝好的showhide.js 包含無動畫、css3動畫、js動畫 包括:fade(淡入淡出) slideUpDown(上下滑動) slideLeftRight(左右滑動) fadeSlideUpDown(淡入淡出+上下滑動) fadeSlideLeftRight (淡入淡出+左右滑動) (fu ...
  • 一般是叫reset.css 我這邊命名成base.css 哎呀無所謂…… @charset "UTF-8"; /*css reset*/ /*清除內外邊距*/ body, h1, h2, h3, h4, h5, h6, p, hr, /*結構元素*/ ul, ol, li, dl, dt, dd, ...
  • transition相容性: 封裝一個js來驗證瀏覽器是否相容transition,以及選擇相容的寫法 以下是transition.js (function(){ //判斷transition屬性是否存在 //存在:空字元串 //不存在:undefined //console.log(documen ...
  • 在vue中,<input type="text" readonly="readonly" @click=""/>,點擊跳轉頁面。 在蘋果手機微信中,會滾動會頂部,才跳轉。滾動你喵呀。 經過各種測試,都不行,後來搜索了input的禁止輸入方法,發現: 換成這樣既可防止蘋果微信瀏覽器滾動,<input ...
  • 1.給元素的某個事件行為綁定方法,事件觸發,方法執行,此時方法中的this一般都是當前元素本身: <div id="div"></div> div.onclick = function() { console.log(this); //=><div id="div"></div> }; div.ad ...
  • 摘要 在React項目中,我們需要採用它的路由庫React-Router來進行頁面跳轉,React會根據路由URL來判斷是哪個頁面。常見的的URL有兩種顯示方式,一種是hashHistory的形式,形如:localhost:3000/#/free-lesson的路由,另一種是browserHisto ...
  • 前言 項目啟動時 css 應該註意哪些問題 文件名規範 文件名建議用小寫字母加中橫線的方式。為什麼呢?因為這樣可讀性比較強,看起來比較清爽,像鏈接也是用這樣的方式,例如 那為什麼變數名不用小寫字母加小劃線的方式,如:family_tree,而是推薦用駝峰式的familyTree?C語言就喜歡用這種方 ...
一周排行
    -Advertisement-
    Play Games
  • GoF之工廠模式 @目錄GoF之工廠模式每博一文案1. 簡單說明“23種設計模式”1.2 介紹工廠模式的三種形態1.3 簡單工廠模式(靜態工廠模式)1.3.1 簡單工廠模式的優缺點:1.4 工廠方法模式1.4.1 工廠方法模式的優缺點:1.5 抽象工廠模式1.6 抽象工廠模式的優缺點:2. 總結:3 ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 本章將和大家分享ES的數據同步方案和ES集群相關知識。廢話不多說,下麵我們直接進入主題。 一、ES數據同步 1、數據同步問題 Elasticsearch中的酒店數據來自於mysql資料庫,因此mysql數據發生改變時,Elasticsearch也必須跟著改變,這個就是Elasticsearch與my ...
  • 引言 在我們之前的文章中介紹過使用Bogus生成模擬測試數據,今天來講解一下功能更加強大自動生成測試數據的工具的庫"AutoFixture"。 什麼是AutoFixture? AutoFixture 是一個針對 .NET 的開源庫,旨在最大程度地減少單元測試中的“安排(Arrange)”階段,以提高 ...
  • 經過前面幾個部分學習,相信學過的同學已經能夠掌握 .NET Emit 這種中間語言,並能使得它來編寫一些應用,以提高程式的性能。隨著 IL 指令篇的結束,本系列也已經接近尾聲,在這接近結束的最後,會提供幾個可供直接使用的示例,以供大伙分析或使用在項目中。 ...
  • 當從不同來源導入Excel數據時,可能存在重覆的記錄。為了確保數據的準確性,通常需要刪除這些重覆的行。手動查找並刪除可能會非常耗費時間,而通過編程腳本則可以實現在短時間內處理大量數據。本文將提供一個使用C# 快速查找並刪除Excel重覆項的免費解決方案。 以下是實現步驟: 1. 首先安裝免費.NET ...
  • C++ 異常處理 C++ 異常處理機制允許程式在運行時處理錯誤或意外情況。它提供了捕獲和處理錯誤的一種結構化方式,使程式更加健壯和可靠。 異常處理的基本概念: 異常: 程式在運行時發生的錯誤或意外情況。 拋出異常: 使用 throw 關鍵字將異常傳遞給調用堆棧。 捕獲異常: 使用 try-catch ...
  • 優秀且經驗豐富的Java開發人員的特征之一是對API的廣泛瞭解,包括JDK和第三方庫。 我花了很多時間來學習API,尤其是在閱讀了Effective Java 3rd Edition之後 ,Joshua Bloch建議在Java 3rd Edition中使用現有的API進行開發,而不是為常見的東西編 ...
  • 框架 · 使用laravel框架,原因:tp的框架路由和orm沒有laravel好用 · 使用強制路由,方便介面多時,分多版本,分文件夾等操作 介面 · 介面開發註意欄位類型,欄位是int,查詢成功失敗都要返回int(對接java等強類型語言方便) · 查詢介面用GET、其他用POST 代碼 · 所 ...
  • 正文 下午找企業的人去鎮上做貸後。 車上聽同事跟那個司機對罵,火星子都快出來了。司機跟那同事更熟一些,連我在內一共就三個人,同事那一手指桑罵槐給我都聽愣了。司機也是老社會人了,馬上聽出來了,為那個無辜的企業經辦人辯護,實際上是為自己辯護。 “這個事情你不能怪企業。”“但他們總不能讓銀行的人全權負責, ...