search
HomeWeb Front-endJS TutorialHow to achieve focus map effect using js

How to achieve focus map effect using js

Jun 20, 2018 pm 05:44 PM
Native jsfocus map

The editor below will share with you an example of a simple focus map effect implemented in native js. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.

Use some packaged motion functions, mainly timers.

The effect is to automatically change pictures and picture descriptions at regular intervals.

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title></title>
 <style type="text/css">
  ul,
  li,
  p,
  h3 {
  padding: 0;
  margin: 0;
  list-style: none;
  }
  
  img {
  border: none;
  vertical-align: top;
  }
  
  #bg_box {
  width: 1000px;
  height: 590px;
  margin: 50px auto;
  position: relative;
  background: url(img/bg1.jpg) no-repeat;
  }
  
  .pic {
  width: 440px;
  height: 274px;
  position: absolute;
  top: 50px;
  left: 220px;
  overflow: hidden;
  }
  
  .li_box {
  width: 1760px;
  height: 274px;
  position: absolute;
  left: 0;
  }
  
  .tags {
  width: 440px;
  height: 80px;
  position: absolute;
  bottom: -80px;
  background-image: linear-gradient(0deg, rgba(0, 0, 0, 0.4) 0%, rgba(255, 255, 255, 0) 100%);
  color: white;
  padding-left: 20px;
  padding-top: 15px;
  box-sizing: border-box;
  }
  
  .tags:nth-of-type(1) {
  /*bottom: 0;*/
  }
  
  .img {
  float: left;
  width: 440px;
  height: 274px;
  }
  
  h3 {
  font: bold 20px/30px "微软雅黑";
  }
  
  p {
  font: 16px/30px "微软雅黑";
  }
 </style>
 <script src="tween.js"></script>
 <script src="commom.js"></script>
 <script type="text/javascript">
  window.onload = function() {
  //获取元素
  var liBbox = $(&#39;ul&#39;)[0];
  var li = $(&#39;li&#39;);
  var tags = $(&#39;.tags&#39;)
  var num = 0; //设置初始位置
  Change()
  function Change() {
   var M = tags[num];
   MTween(M, &#39;bottom&#39;, 0, 500, &#39;px&#39;, &#39;linear&#39;, function() { //先让描述内容出现
   num++
   if(num > li.length - 1) { //边界设置。
    return;
   }
   setTimeout(function() {
    MTween(M, &#39;bottom&#39;, -80, 500, &#39;px&#39;, &#39;linear&#39;, function() { //让描述内容不显示
    MTween(liBbox, &#39;left&#39;, -num * 440, 800, &#39;px&#39;, &#39;linear&#39;, function() {
     Change();
    }); //切换图片
    });
   }, 1000)
   });
  }
  }
 </script>
 </head>
 <body>
 <section id="bg_box">
  <p class="pic">
  <ul class="li_box">
   <li>
   <img  class="img lazy"  src="/static/imghwm/default1.png"  data-src="img/a5.gif"    alt="How to achieve focus map effect using js" >
   </li>
   <li>
   <img  class="img lazy"  src="/static/imghwm/default1.png"  data-src="img/a6.gif"    alt="How to achieve focus map effect using js" >
   </li>
   <li>
   <img  class="img lazy"  src="/static/imghwm/default1.png"  data-src="img/a7.gif"    alt="How to achieve focus map effect using js" >
   </li>
   <li>
   <img  class="img lazy"  src="/static/imghwm/default1.png"  data-src="img/a8.gif"    alt="How to achieve focus map effect using js" >
   </li>
  </ul>
  <p class="tags">
   <h3 id="下雨了">下雨了~~~</h3>
   <p class="tag">这是一个适合在家睡觉的日子!!</p>
  </p>
  <p class="tags">
   <h3 id="包饺子">包饺子~~~</h3>
   <p class="tag">一只会居家过日子的小狐狸!!</p>
  </p>
  <p class="tags">
   <h3 id="生气了">生气了~~~</h3>
   <p class="tag">吃掉好吃的就不生气了!!</p>
  </p>
  <p class="tags">
   <h3 id="出发了">出发了~~~</h3>
   <p class="tag">来一段说走就走的旅行!!</p>
  </p>
  </p>
 </section>
 </body>
</html>

commom.js

function MTween(obj,attr,end,duration,unit,way,callBack){
 if(obj.isAnim) return;
 //obj开始运动了 自定义属性
 obj.isAnim = true;
 if(!way){ //如果用户没有选择运动方式就默认匀速
 way = &#39;linear&#39;;
 }
 if(!unit){ //如果用户没有选择运动方式就默认匀速
 unit = &#39;&#39;;
 }
 var start = parseFloat(getStyle(obj,attr));//起始位置
// var end = 1000;//目标点
// var duration = 1000;//动画执行的总时间 单位是毫秒
 var startTime = Date.now();
 var s = end - start; //总路程
// var v = s/duration; //计算出来的速度
 //每次20ms走一帧
 clearInterval(timer);
 var timer = 0;
 timer = setInterval(function(){
 var endTime = Date.now();
 //计算出当前时间
 var t = endTime-startTime;
 if(t>=duration){
  t = duration;
  clearInterval(timer);//到达目标点要清除定时器
 }
//  obj.style[attr] = t*s/duration+start+&#39;px&#39;;
// console.log(Tween[way](t,start,s,duration))
 obj.style[attr] = Tween[way](t,start,s,duration)+unit;
 
  //透明度的兼容处理
  if(attr==&#39;opacity&#39;){
  obj.style.filter = &#39;Alpha(opacity=&#39;+Tween[way](t,start,s,duration)*100+&#39;)&#39;;
  }
 if(t==duration){
  obj.isAnim = false;
  //等到上一个动画完成 然后再调用
  if(callBack){
  callBack();
  }
 }
 },20);
}

tween.js

/*
* t : time 已过时间 当前时间-初始时间
* b : begin 起始值
* c : count 总的运动值 总路程
* d : duration 持续时间 总时间
*
* s = vt; => c = t*c/d 这里只计算总共要运动的路程 ,不包括起始位置
*
* attrVal = t*c/d + b;
*
* 曲线方程
*
* */
//Tween.linear();
var Tween = {
 linear: function (t, b, c, d){ //匀速
 return c*t/d + b;
 },
 easeIn: function(t, b, c, d){ //加速曲线
 return c*(t/=d)*t + b;
 },
 easeOut: function(t, b, c, d){ //减速曲线
 return -c *(t/=d)*(t-2) + b;
 },
 easeBoth: function(t, b, c, d){ //加速减速曲线
 if ((t/=d/2) < 1) {
  return c/2*t*t + b;
 }
 return -c/2 * ((--t)*(t-2) - 1) + b;
 },
 easeInStrong: function(t, b, c, d){ //加加速曲线
 return c*(t/=d)*t*t*t + b;
 },
 easeOutStrong: function(t, b, c, d){ //减减速曲线
 return -c * ((t=t/d-1)*t*t*t - 1) + b;
 },
 easeBothStrong: function(t, b, c, d){ //加加速减减速曲线
 if ((t/=d/2) < 1) {
  return c/2*t*t*t*t + b;
 }
 return -c/2 * ((t-=2)*t*t*t - 2) + b;
 },
 elasticIn: function(t, b, c, d, a, p){ //正弦衰减曲线(弹动渐入)
 if (t === 0) { 
  return b; 
 }
 if ( (t /= d) == 1 ) {
  return b+c; 
 }
 if (!p) {
  p=d*0.3; 
 }
 if (!a || a < Math.abs(c)) {
  a = c; 
  var s = p/4;
 } else {
  var s = p/(2*Math.PI) * Math.asin (c/a);
 }
 return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
 },
 elasticOut: function(t, b, c, d, a, p){ //*正弦增强曲线(弹动渐出)
 if (t === 0) {
  return b;
 }
 if ( (t /= d) == 1 ) {
  return b+c;
 }
 if (!p) {
  p=d*0.3;
 }
 if (!a || a < Math.abs(c)) {
  a = c;
  var s = p / 4;
 } else {
  var s = p/(2*Math.PI) * Math.asin (c/a);
 }
 return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
 }, 
 elasticBoth: function(t, b, c, d, a, p){
 if (t === 0) {
  return b;
 }
 if ( (t /= d/2) == 2 ) {
  return b+c;
 }
 if (!p) {
  p = d*(0.3*1.5);
 }
 if ( !a || a < Math.abs(c) ) {
  a = c; 
  var s = p/4;
 }
 else {
  var s = p/(2*Math.PI) * Math.asin (c/a);
 }
 if (t < 1) {
  return - 0.5*(a*Math.pow(2,10*(t-=1)) * 
   Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
 }
 return a*Math.pow(2,-10*(t-=1)) * 
  Math.sin( (t*d-s)*(2*Math.PI)/p )*0.5 + c + b;
 },
 backIn: function(t, b, c, d, s){ //回退加速(回退渐入)
 if (typeof s == &#39;undefined&#39;) {
  s = 1.70158;
 }
 return c*(t/=d)*t*((s+1)*t - s) + b;
 },
 backOut: function(t, b, c, d, s){
 if (typeof s == &#39;undefined&#39;) {
  s = 3.70158; //回缩的距离
 }
 return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
 }, 
 backBoth: function(t, b, c, d, s){
 if (typeof s == &#39;undefined&#39;) {
  s = 1.70158; 
 }
 if ((t /= d/2 ) < 1) {
  return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
 }
 return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
 },
 bounceIn: function(t, b, c, d){ //弹球减振(弹球渐出)
 return c - Tween[&#39;bounceOut&#39;](d-t, 0, c, d) + b;
 }, 
 bounceOut: function(t, b, c, d){//*
 if ((t/=d) < (1/2.75)) {
  return c*(7.5625*t*t) + b;
 } else if (t < (2/2.75)) {
  return c*(7.5625*(t-=(1.5/2.75))*t + 0.75) + b;
 } else if (t < (2.5/2.75)) {
  return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b;
 }
 return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b;
 }, 
 bounceBoth: function(t, b, c, d){
 if (t < d/2) {
  return Tween[&#39;bounceIn&#39;](t*2, 0, c, d) * 0.5 + b;
 }
 return Tween[&#39;bounceOut&#39;](t*2-d, 0, c, d) * 0.5 + c*0.5 + b;
 }
}

The above is what I compiled for you. I hope it will be helpful to everyone in the future.

Related articles:

Detailed introduction to the proxy mode (Proxy) in ES6

Detailed introduction to building webpack

How to implement file packaging using webpack

Using webpack to build vue scaffolding

How to implement calendar using Vue components (details Tutorial)

The above is the detailed content of How to achieve focus map effect using js. For more information, please follow other related articles on the PHP Chinese website!

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
The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

See all articles

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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),