开言
本篇文章通过开源html5引擎lufylegend实现JQuery滑动效果。能使一个矩形来回减速加速滑行。由于用到了html5里的canvas,所以如果大家要测试程序,请用支持html5的浏览器打开。源码下载稍后提供。
初始化页面
首先我们来看看html文件里的代码:
> <html> <head> <meta charset="utf-8" /> <title>html5 game - 仿jquerytitle> <script type="text/javascript" src="./js/lufylegend-1.6.1.min.js">script> <script type="text/javascript" src="./js/main.js">script> head> <body> <div id="mylegend">loading……div> body> html>
也许有人疑问,说是用html5,怎么不见canvas标签???其实当你使用lufylegend开发时,根本不需要加入canvas标签。只需要调用该引擎中的init函数并向参数赋值就能自动将canvas标签加到html文件中。
接下来让我们看看init用法:init(speed,id,width,height,function,type);其中speed代表页面更换速度,id代表传入的一个div的id,库件进行初始化的时候,会自动将canvas加入到此div内部,width和height分别代表页面大小,function就是初始化完成后调用的函数,最后一个参数type为null时,会先进行页面的onload操作,如果init函数调用是在onload之后,那么需要将此参数设为LEvent.INIT。
我在js里是这样调用init的:
init(50,"mylegend",800,400,main);
开始移动
看完初始化部分,我们不仿来看看是怎样做到能使一个矩形来回减速加速滑行。首先我们来看看main.js里的代码:
init(50,"mylegend",800,400,main); var backLayer; var speed = 20,x = 0; function main(){ //加入层 initLayer(); //开始移动 setInterval(function(){move();},10); } function initLayer(){ //加入背景层 backLayer = new LSprite(); addChild(backLayer); } function move(){ //清空背景 backLayer.graphics.drawRect(0,"white",[0,0,800,400],true,"white"); //绘制矩形 backLayer.graphics.drawRect(0,"dimgray",[x+=speed,0,200,200],true,"gray"); //将速度逐渐变小 speed--; //当速度小于或等于-20时,将速度重新设为20 if(speed <= -20){ speed = 20; } }
当我初始化完毕后,我加入了层,也就LSprite,用法如下:
■作用:
LSprite 类是基本显示列表构造块,一个可显示图形并且也可包含子项的显示列表节点。
■可用属性:
type:类型
x:坐标x
y:坐标y
scaleX:X坐标方向上的缩放比例
alpha:透明度
rotate:旋转角度
visible:是否可见,当设为false的时候,该LBitmap对象不可视,且内部所有处理都将停止
childList:该对象的所有子项
graphics:指定属于此 LSprite 的 Graphics 对象,在此 LSprite 中可执行矢量绘图命令。
box2dBody:结合box2dweb后,创建的body2d
mask:遮罩
举个例子:
var loader; init(200,"mylegend",500,350,main); function main(){ loader = new LLoader(); loader.addEventListener(LEvent.COMPLETE,loadBitmapdata); loader.load("img.png","bitmapData"); } function loadBitmapdata(event){ var bitmapdata = new LBitmapData(loader.content,0,0,64,64); var bitmap = new LBitmap(bitmapdata); var layer = new LSprite(); addChild(layer); layer.addChild(bitmap); }
当层初始化完成后,就可以开始移动矩形了。首先我用setInterval不断地画矩形,为了不让矩形重叠,我让它每画一次,清空一次屏幕。
//清空背景 backLayer.graphics.drawRect(0,"white",[0,0,800,400],true,"white"); //绘制矩形 backLayer.graphics.drawRect(0,"dimgray",[x+=speed,0,200,200],true,"gray");
以上代码实现了不断画矩形。可以看到,我是在backLayer层上实行绘制的。至于graphics的用法,我将它列在下面:
drawRect( thickness, lineColor, pointArray, isfill, color )
■作用:
画一个矩形
■参数:
thickness:边缘线粗
lineColor:边缘线颜色
pointArray:[起始坐标x,起始坐标y,矩形宽width,矩形高height]
isfill:是否实心图形
color:实心颜色
接下来就是改变速度。首先我将每次移动的速度减少1,然后进行一次重新绘制。这样的画就可以进行减速加速滑行。速度减少成了负数就会往反方向走,但如果减少的太多就会移出屏幕,所以给它一个限制:
if(speed <= -20){ <span style="white-space:pre"> </span>speed = 20; }
也就是说如果减少的速度达到了-20那就让矩形往回跑。
这样一来,来回滑动就做好了。
以上就是HTML5制作仿jQuery效果的内容,更多相关内容请关注PHP中文网(www.php.cn)!

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)