JavaScript: GIFless animation. JavaScript: ภาพเคลื่อนไหว GIFless. Animate images,logos with jQuery ภาพชีวิตโลโก้กับ jQuery
Posted on 23. โพสต์ใน 23. Jul, 2009 by Dragos in Coding , Design , JavaScript & Ajax ก.ค., 2,009 โดย Dragos ใน การเข้ารหัส, Design, JavaScript และ Ajax
After playing some time with position relative and absolute, I've noticed one a great aspect of the png image format (probably gif images are also good, but since we are trying to create a GIFless animation, I won't use any gif images in this tutorial). หลังจากเล่นเวลามีตำแหน่งญาติและแน่นอนเราได้พบหนึ่งด้านดีของรูปแบบภาพ png (ภาพ gif อาจยังดีแต่เนื่องจากเรากำลังพยายามสร้างภาพเคลื่อนไหว GIFless ฉันจะไม่ใช้ภาพ gif ใดในกวดวิชา).
So here's aa PNG (transparent text), with the name of this website: PNG aa ดังนั้นนี่ (ข้อความโปร่งใส) ที่มีชื่อของเว็บไซต์นี้:
The trick of animating images, without the need of using the GIF image format features, are as follow: เคล็ดลับของ animating ภาพโดยไม่ต้องใช้ GIF คุณสมบัติรูปแบบภาพมีดังนี้
We have an image with transparent text written on it. เรามีภาพโปร่งใสกับข้อความที่เขียนบน. To animate the transparent text, we need to place a background image below the actual image and move this background image, in order to create a so called “animation”. เพื่อกระตุ้นข้อความที่โปร่งใสเราต้องใส่ภาพพื้นหลังด้านล่างภาพที่เกิดขึ้นจริงและย้ายรูปภาพพื้นหลังนี้เพื่อสร้างภาพเคลื่อนไหว "จึงเรียก". To be more precise, the image we want to be animated should have a higher z-index than the background image below it (it's like working with layers in Photoshop). จะเฉพาะเจาะจงมาขึ้นภาพที่เราต้องการจะเคลื่อนไหวควรมี z ดัชนีสูงกว่ารูปภาพพื้นหลังด้านล่างมัน (ก็ชอบทำงานกับชั้นใน Photoshop). In order to animate the background image we need some javascript work to be done. เพื่อชีวิตรูปภาพพื้นหลังที่เราต้องมีงานจาวาสคริปต์เพื่อทำ. Of course, as a big fan of jQuery, I've used jQuery to do all the animation stuff. แน่นอนเป็นแฟนใหญ่ของ jQuery เราได้ใช้ jQuery จะทำทุกสิ่งเคลื่อนไหว.
Here's the script I've come up with: นี่คือสคริปต์ฉันได้มาด้วย:
(function($){ $.fn.enliven = function(options) { var defaults = { bgImage: '', duration:3000 }; var options = $.extend(defaults, options); var imH=this.attr("height"), imW=this.attr("width"); this.wrap(' '); this.css("position","absolute"); $('#'+this.attr("className")+'Cont').append(' '); var maxLeft=($('.lbg'+this.attr("className")).attr('clientWidth')-this.attr('width'))/2, maxTop=($('.lbg'+this.attr("className")).attr('clientHeight')-this.attr('height'))/2; function getrand() { var nr=Math.floor(Math.random()*2); return nr==0?-1:1; } function randimate(obj) { var ph=Math.floor(imH*20/100),pw=Math.floor(imW*20/100); newLeft=$(".lbg"+obj.attr("className")).attr("offsetLeft")+Math.floor(Math.random()*pw)*getrand(); newTop=$(".lbg"+obj.attr("className")).attr("offsetTop")+Math.floor(Math.random()*ph)*getrand(); if(newLeft>0) newLeft=0; else if(newLeft<-maxLeft) newLeft=-maxLeft+1; if(newTop>0) newTop=0; else if(newTop<-maxTop) newTop=-maxTop+1; $('.lbg'+obj.attr("className")).animate({left:newLeft+"px",top:newTop+"px"},defaults.duration,"linear",function(){ randimate(obj); }); } return this.each(function(){ randimate($(this)); }); }; })(jQuery); ค่าเริ่มต้น (ทำงาน ($) $ (. fn.enliven ฟังก์ชัน = (ตัวเลือก) (var = (bgImage:''ระยะเวลา: 3000); ตัวเลือก var = $. ขยาย (ค่าเริ่มต้นตัวเลือก) imH var = this.attr ( ความสูง ") imW = this.attr (กว้าง" "); this.wrap ( ''); this.css (ตำแหน่ง" "" แน่นอน "); $('#'+ this.attr (className" " ) + 'Cont'). ต่อท้าย ( ''); maxLeft var =($('. lbg '+ this.attr (className "")) attr. clientWidth (' ') this.attr (' กว้าง ')) / 2, maxTop lbg =($('. '+ this.attr (className "")). attr clientHeight (' ') this.attr (' สูง ')) / 2; getrand ฟังก์ชัน () nr (var = Math.floor (Math.random () * 2) nr กลับ == 0? -1:1;) ฟังก์ชัน randimate (obj) (var pH = Math.floor (imH * 20/100) pw = Math.floor (imW * 20/100) newLeft =$(". lbg "+ obj.attr (className" ")). attr (offsetLeft" ") + Math.floor (Math.random () pw *) getrand * () ; newTop lbg =$(". "+ obj.attr (className" ")). attr (offsetTop") + Math.floor (Math.random () * pH) getrand * () ถ้า (newLeft> 0) newLeft = 0; อื่นถ้า (newLeft <-newLeft) maxLeft =- +1 maxLeft; ถ้า (newTop> 0) newTop = 0; อื่นถ้า (newTop <-maxTop newTop) =- maxTop +1; $ ( 'lbg.' + obj.attr (className "")). ชีวิต ((ซ้าย: newLeft + "px" ด้านบน newTop + "px") defaults.duration, "เส้นตรง" ฟังก์ชัน () (randimate (obj);));) this.each กลับ (ฟังก์ชัน () (randimate ($ (นี้));)));)) (jQuery) The code above works as a plugin of jQuery, so it should be included in the page, after the inclusion of jQuery. รหัสข้างบนทำงานเป็นโปรแกรมเสริมของ jQuery ดังนั้นควรจะรวมอยู่ในหน้านี้หลังจากที่รวมของ jQuery. In order to animate an image, you should use this code: เพื่อกระตุ้นภาพคุณควรใช้รหัสนี้
$('image_selector').enliven({bgImage:"./path_to_background_image.png",duration:1000}); //duration is the time of one time background animation $ image_selector ( ''). ทำให้มีชีวิตชีวา ((bgImage: ". / path_to_background_image.png" ระยะเวลา: 1000)) / / ระยะเวลาหนึ่งภาพเคลื่อนไหวพื้นหลังเวลา Here's a crazy example 1: http://iwebdevel.com/wp-content/uploads/2009/07/logo.php นี่เป็นตัวอย่างของบ้า 1: http://iwebdevel.com/wp-content/uploads/2009/07/logo.php
Here's another non-crazy example 2: http://iwebdevel.com/wp-content/uploads/2009/07/logo2.php นี่ไม่ใช่อื่น-example บ้า 2: http://iwebdevel.com/wp-content/uploads/2009/07/logo2.php
If you like the idea and want more features, drop me a line in a comment and I'll try to improve the code and add more features ถ้าต้องการความคิดและต้องการคุณสมบัติเพิ่มเติมวางฉันบรรทัดในความคิดเห็นและฉันจะพยายามปรับปรุงรหัสและเพิ่มคุณสมบัติมากขึ้น ![]()
Related posts: บทความที่เกี่ยวข้อง:
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: ถ้าเคลื่อนไหว jQuery จะไฟไม่ / เริ่ม?
- JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: วิธีการรับดัชนี (ตำแหน่งภายในกลุ่ม) ของวัตถุด้วย jQuery?
- Javascript: How to validate email address with JavaScript? Javascript: การตรวจสอบที่อยู่อีเมลด้วย JavaScript?
- JavaScript: Get anchor from URL JavaScript: Get ทอดสมอจาก URL
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript: ส่งหน้าที่เป็นพารามิเตอร์ทำงาน (callbacks อื่น)













































