Home php教程 php手册 JavaScript 图片滑动切换效果

JavaScript 图片滑动切换效果

Jul 11, 2016 pm 08:00 PM
javascript code switch picture Open source Effect slide programming programming language software development

序一(08/07/06)

看到alibaba的一个图片切换效果,感觉不错,想拿来用用。但代码一大堆的,看着昏,还是自己来吧。
由于有了做图片滑动展示效果的经验,做这个就容易得多了。


序二(09/03/19)

自写了Tween缓动之后就很想重新写过这个效果,近来有点时间,终于可以动手了。
除了改成Tween缓动,也改进了程序算法,不过原理还是一样的。


效果预览

<script> new SlideTrans("idContainer", "idSlider", 3, { Vertical: false }).Run(); </script>
仿淘宝/alibaba图片切换:
  • 图片上传预览
  • 多级联动菜单
  • 浮动定位提示
  • 数据延迟加载
  • 简便文件上传

<script> var nums = [], timer, n = $$("idSlider2").getElementsByTagName("li").length, st = new SlideTrans("idContainer2", "idSlider2", n, { onStart: function(){//设置按钮样式 forEach(nums, function(o, i){ o.className = st.Index == i ? "on" : ""; }) } }); for(var i = 1; i <= n; AddNum(i++)){}; function AddNum(i){ var num = $$("idNum").appendChild(document.createElement("li")); num.innerHTML = i--; num.onmouseover = function(){ timer = setTimeout(function(){ num.className = "on"; st.Auto = false; st.Run(i); }, 200); } num.onmouseout = function(){ clearTimeout(timer); num.className = ""; st.Auto = true; st.Run(); } nums[i] = num; } st.Run(); $$("idAuto").onclick = function(){ if(st.Auto){ st.Auto = false; st.Stop(); this.value = "自动"; }else{ st.Auto = true; st.Run(); this.value = "停止"; } } $$("idNext").onclick = function(){ st.Next(); } $$("idPre").onclick = function(){ st.Previous(); } $$("idTween").onchange = function(){ switch (parseInt(this.value)){ case 2 : st.Tween = Tween.Bounce.easeOut; break; case 1 : st.Tween = Tween.Back.easeOut; break; default : st.Tween = Tween.Quart.easeOut; } } </script>

 


程序说明

原理就是通过不断设置滑动对象的left(水平切换)和top(垂直切换)来实现图片切换的动态效果。

首先需要一个容器,程序会自动设置容器overflow为hidden,如果不是相对或绝对定位会同时设置position为relative,
滑动对象会设置为绝对定位:

var p = CurrentStyle(this._container).position;
== "relative" || p == "absolute" || (this._container.style.position = "relative");
this._container.style.overflow = "hidden";
this._slider.style.position = "absolute";


如果没有设置Change切换参数属性,会自动根据滑动对象获取:

this.Change = this.options.Change ? this.options.Change :
    
this._slider[bVertical ? "offsetHeight" : "offsetWidth"/ this._count;


执行Run方法就会开始进入切换,其中有一个可选参数用来重新设置要切换的图片索引:

index == undefined && (index = this.Index);
index 
 0 && (index = this._count - 1|| index >= this._count && (index = 0);

之后就到设置使用tween缓动时需要的参数了,
包括_target(目标值)、_t(时间)、_b(初始值)和_c(变化量):

JavaScript 图片滑动切换效果JavaScript 图片滑动切换效果Code


还有Duration(持续时间)是自定义属性。

参数设置好后就执行Move程序开始移动了。
里面很简单,首先判断_c是否有值(等于0表示不需要移动)和_t是否到达Duration,
未满足条件就继续移动,否则直接移动到目标值并进行下一次切换:

JavaScript 图片滑动切换效果JavaScript 图片滑动切换效果Code

 

使用说明

实例化需要3个参数,分别是容器对象,滑动对象和切换数量,之后可以直接执行Run方法运行:

new SlideTrans("idContainer""idSlider"3).Run();


还有以下可选属性:
Vertical: true,//是否垂直方向(方向不能改)
Auto:  true,//是否自动
Change:  0,//改变量
Duration: 50,//滑动持续时间
Time:  10,//滑动延时
Pause:  2000,//停顿时间(Auto为true时有效)
onStart: function(){},//开始转换时执行
onFinish: function(){},//完成转换时执行
Tween:  Tween.Quart.easeOut//tween算子

其中Vertical初始化后就不能修改,Tween算子可参照这里的缓动效果选择(实例中选了其中3个)。

还有提供了以下方法:
Next: 切换下一个
Previous: 切换上一个
Stop: 停止自动切换
还有上面说到的Run

 

程序代码

JavaScript 图片滑动切换效果JavaScript 图片滑动切换效果Code

 


下载测试代码

其他图片展示效果:
JavaScript 图片变换效果(ie only)
JavaScript 图片滑动展示效果

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Huawei's official introductory tutorial for Cangjie programming language is released. Learn how to obtain the universal version SDK in one article Huawei's official introductory tutorial for Cangjie programming language is released. Learn how to obtain the universal version SDK in one article Jun 25, 2024 am 08:05 AM

According to news from this site on June 24, at the keynote speech of the HDC2024 Huawei Developer Conference on June 21, Gong Ti, President of Huawei Terminal BG Software Department, officially announced Huawei’s self-developed Cangjie programming language. This language has been developed for 5 years and is now available for developer preview. Huawei's official developer website has now launched the official introductory tutorial video of Cangjie programming language to facilitate developers to get started and understand it. This tutorial will take users to experience Cangjie, learn Cangjie, and apply Cangjie, including using Cangjie language to estimate pi, calculate the stem and branch rules for each month of 2024, see N ways of expressing binary trees in Cangjie language, and use enumeration types to implement Algebraic calculations, signal system simulation using interfaces and extensions, and new syntax using Cangjie macros, etc. This site has tutorial access address: ht

After 5 years of research and development, Huawei's next-generation programming language 'Cangjie” has officially launched its preview After 5 years of research and development, Huawei's next-generation programming language 'Cangjie” has officially launched its preview Jun 22, 2024 am 09:54 AM

This site reported on June 21 that at the HDC2024 Huawei Developer Conference this afternoon, Gong Ti, President of Huawei Terminal BG Software Department, officially announced Huawei’s self-developed Cangjie programming language and released a developer preview version of HarmonyOSNEXT Cangjie language. This is the first time Huawei has publicly released the Cangjie programming language. Gong Ti said: "In 2019, the Cangjie programming language project was born at Huawei. After 5 years of R&D accumulation and heavy R&D investment, it finally meets global developers today. Cangjie programming language integrates modern language features, comprehensive compilation optimization and Runtime implementation and out-of-the-box IDE tool chain support create a friendly development experience and excellent program performance for developers. "According to reports, Cangjie programming language is an all-scenario intelligence tool.

Tianjin University and Beihang University are deeply involved in Huawei's 'Cangjie” project and launched the first AI agent programming framework 'Cangqiong” based on domestic programming languages. Tianjin University and Beihang University are deeply involved in Huawei's 'Cangjie” project and launched the first AI agent programming framework 'Cangqiong” based on domestic programming languages. Jun 23, 2024 am 08:37 AM

According to news from this site on June 22, Huawei yesterday introduced Huawei’s self-developed programming language-Cangjie to developers around the world. This is the first public appearance of Cangjie programming language. According to inquiries on this site, Tianjin University and Beijing University of Aeronautics and Astronautics were deeply involved in the research and development of Huawei’s “Cangjie”. Tianjin University: Cangjie Programming Language Compiler The software engineering team of the Department of Intelligence and Computing of Tianjin University joined hands with the Huawei Cangjie team to deeply participate in the quality assurance research of the Cangjie programming language compiler. According to reports, the Cangjie compiler is the basic software that is symbiotic with the Cangjie programming language. In the preparatory stage of the Cangjie programming language, a high-quality compiler that matches it became one of the core goals. As the Cangjie programming language evolves, the Cangjie compiler is constantly being upgraded and improved. In the past five years, Tianjin University

The screen is good for playing games. Brief analysis of iQOO Neo9S Pro+ screen The screen is good for playing games. Brief analysis of iQOO Neo9S Pro+ screen Jul 19, 2024 pm 03:53 PM

In today's smartphone market, screen quality has become one of the key indicators to measure the overall performance of a mobile phone. iQOO's Neo series has always been committed to providing users with excellent gaming experience and visual enjoyment. The latest product iQOO Neo9SPro+ uses a "Three Good Eye Protection Gaming Screen". Next, let's take a look at the quality of this screen. How brilliant. iQOO Neo9S Pro+ is equipped with a 1.5 KOLED e-sports direct screen, which supports flagship LTPO adaptive refresh rate from 1Hz to 144Hz, which means that it can achieve ultra-low power standby state when displaying static content, and it can also be intelligent during gaming. Switch to dynamic high from 90Hz to 144Hz

OPPO Find X7 is a masterpiece! Capture your every moment with images OPPO Find X7 is a masterpiece! Capture your every moment with images Aug 07, 2024 pm 07:19 PM

In this fast-paced era, OPPO Find X7 can use its imaging power to let us savor every beautiful moment in life. Whether it's magnificent mountains, rivers, lakes, or seas, warm family gatherings, or encounters and surprises on the street, it can help you record them with "unparalleled" picture quality. From the outside, the camera Deco design of Find It looks very recognizable and has a high-end feel. The inside is also unique, starting with the basic hardware configuration. FindX7 maintains the previous

Huawei: Cangjie programming language is independently controllable and does not evolve based on any existing programming language Huawei: Cangjie programming language is independently controllable and does not evolve based on any existing programming language Jun 22, 2024 pm 12:26 PM

According to news from this site on June 21, Huawei’s self-developed Cangjie programming language was officially unveiled today, and the HarmonyOSNEXT Cangjie language developer preview version Beta recruitment was launched. The Cangjie Programming Language Q&A page on Huawei's official website shows that Cangjie Programming Language is a next-generation application programming language oriented to all-scenario intelligence. It focuses on native intelligence, natural all-scenarios, high performance and strong security. It is combined with Hongmeng system to provide good programming. experience. For different business scenarios, Hongmeng Ecosystem provides application developers with multi-language hybrid development capabilities such as Cangjie and ArkTS. Cangjie and ArkTS develop together and form complementary advantages in the Hongmeng ecosystem. Cangjie is more suitable for business scenarios with high performance and high concurrency requirements. The goal of Cangjie programming language is to create Hongmeng applications that can perform tasks concurrently.

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

ChinaJoy2024 appears in another world Evnia 49-inch giant screen debut ChinaJoy2024 appears in another world Evnia 49-inch giant screen debut Jul 25, 2024 pm 12:46 PM

ChinaJoy2024 will be held at the New International Expo Center in Shanghai from July 26 to July 29. As the official cooperative media of ChinaJoy, Zhongguancun Online will take part in this annual event with more than 24 fashion, technology, and digital brands at booth S101 of the E7 exhibition hall, covering a total area of ​​795 square meters, creating a full coverage of vision, hearing, and touch—— "Coordinated in Shanghai! It's trendy and fun here!" Super carnival. Philips and its gaming monitor sub-brand Evnia will also participate in ChinaJoy2024 this time. In addition to the EvniaO8 series QDOLED gaming monitor 32M2N8800 and EvniaM6 series MiniLED we have introduced

See all articles