抽屉式信息栏

简介: 抽屉式信息栏

最后的效果展示:


1.html部分

两个结构,一个是标题,一个是内容

<body>
    <div id="share">
      <div id="share_content">填写的内容</div>
      <div id="share_title">标题</div>
    </div>
  </body>

2.CSS部分

将内容隐藏,只显示标题,当鼠标移动到标题结构时,才会让内容显示

* {
  margin: 0px;
  padding: 0px;
}
#share {
  margin: 0px;
  padding: 0px;
  width: 200px;
  height: 400px;
  position: absolute;
  border: 1px solid #999;
  top: -350px;
}
#share_title {
  width: 100%;
  height: 50px;
  line-height: 50px;
  text-align: center;
  letter-spacing: 4px;
  color: #333;
  border-top: 1px solid #999;
  cursor: pointer;
}
#share_content {
  width: 100%;
  height: 350px;
  font-size: 20px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

3.JS部分

添加动画函数,施加动画效果的对象和参数,循环执行的速率和动画变化的速率。

/**
 * 功能说明:动画函数
 * {Object} obj   施加动画效果的对象
 * {Object} json    施加动画效果的参数
 * {Object} interval  循环执行的速率
 * {Object} sp    动画变化的速率
 * {Object} fn    回调函数
 */
// 获取div
let share = document.getElementById("share");
// 当鼠标移动到内容框时执行动画让内容框向下移动
share.onmouseover = function() {
  animate(this, {
    top: 0
  }, 10, 0.01);
}
// 当鼠标离开到内容框时执行动画让内容框向上隐藏
share.onmouseout = function() {
  animate(this, {
    top: -350
  }, 10, 0.01);
}
// 设置动画效果
function animate(obj, json, interval, sp, fn) {
  clearInterval(obj.timer);
  function getStyle(obj, arr) {
    if (obj.currentStyle) {
      return obj.currentStyle[arr];
    } else {
      return document.defaultView.getComputedStyle(obj, null)[arr];
    }
  }
  obj.timer = setInterval(function() {
    let flag = true;
    for (let arr in json) {
      let current = 0;
      if (arr == "opacity") {
        current = Math.round(parseFloat(getStyle(obj, arr)) * 100);
      } else {
        current = parseInt(getStyle(obj, arr));
      }
      let speed = (json[arr] - current) * sp;
      speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
      if (current != json[arr]) {
        flag = false;
      }
      if (arr == "opacity") {
        obj.style.filter = "alpha(opacity : '+(current + speed)+' )";
        obj.style.opacity = (current + speed) / 100;
      } else {
        obj.style[arr] = current + speed + "px";
      }
    }
    if (flag) {
      clearInterval(obj.timer);
      if (fn) {
        fn();
      }
    }
  }, interval);
}

4.整体部分

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title></title>
  </head>
  <style type="text/css">
    * {
      margin: 0px;
      padding: 0px;
    }
    #share {
      margin: 0px;
      padding: 0px;
      width: 200px;
      height: 400px;
      position: absolute;
      border: 1px solid #999;
      top: -350px;
    }
    #share_title {
      width: 100%;
      height: 50px;
      line-height: 50px;
      text-align: center;
      letter-spacing: 4px;
      color: #333;
      border-top: 1px solid #999;
      cursor: pointer;
    }
    #share_content {
      width: 100%;
      height: 350px;
      font-size: 20px;
      text-align: center;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  </style>
  <body>
    <div id="share">
      <div id="share_content">填写的内容</div>
      <div id="share_title">标题</div>
    </div>
  </body>
  <script>
    /**
     * 功能说明:动画函数
     * {Object} obj   施加动画效果的对象
     * {Object} json    施加动画效果的参数
     * {Object} interval  循环执行的速率
     * {Object} sp    动画变化的速率
     * {Object} fn    回调函数
     */
    // 获取div
    let share = document.getElementById("share");
    // 当鼠标移动到内容框时执行动画让内容框向下移动
    share.onmouseover = function() {
      animate(this, {
        top: 0
      }, 10, 0.01);
    }
    // 当鼠标离开到内容框时执行动画让内容框向上隐藏
    share.onmouseout = function() {
      animate(this, {
        top: -350
      }, 10, 0.01);
    }
    // 设置动画效果
    function animate(obj, json, interval, sp, fn) {
      clearInterval(obj.timer);
      function getStyle(obj, arr) {
        if (obj.currentStyle) {
          return obj.currentStyle[arr];
        } else {
          return document.defaultView.getComputedStyle(obj, null)[arr];
        }
      }
      obj.timer = setInterval(function() {
        let flag = true;
        for (let arr in json) {
          let current = 0;
          if (arr == "opacity") {
            current = Math.round(parseFloat(getStyle(obj, arr)) * 100);
          } else {
            current = parseInt(getStyle(obj, arr));
          }
          let speed = (json[arr] - current) * sp;
          speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
          if (current != json[arr]) {
            flag = false;
          }
          if (arr == "opacity") {
            obj.style.filter = "alpha(opacity : '+(current + speed)+' )";
            obj.style.opacity = (current + speed) / 100;
          } else {
            obj.style[arr] = current + speed + "px";
          }
        }
        if (flag) {
          clearInterval(obj.timer);
          if (fn) {
            fn();
          }
        }
      }, interval);
    }
  </script>
</html>
相关文章
|
1月前
|
计算机视觉 Python
使用跟踪栏
【5月更文挑战第11天】使用跟踪栏。
17 1
|
7月前
|
小程序 JavaScript
小程序点击按钮弹出可填写框
小程序点击按钮弹出可填写框
78 0
|
小程序 JavaScript 数据库
小程序分类页实现三级分类,顶部导航栏,左侧分类栏,右侧数据列表
小程序分类页实现三级分类,顶部导航栏,左侧分类栏,右侧数据列表
177 0
|
前端开发 JavaScript 开发者
分类页-左侧栏 |学习笔记
快速学习 分类页-左侧栏
52 0
分类页-左侧栏 |学习笔记
标签页+标签编辑页
标签页+标签编辑页
SwiftUI—点击列表不同的选项进入不同的详情页面
SwiftUI—点击列表不同的选项进入不同的详情页面
219 0
SwiftUI—点击列表不同的选项进入不同的详情页面
|
前端开发 JavaScript
制作四个选项卡页 Tab,用户可以通过切换不同的 Tab 页查看不同类别的新闻信息,每个 Tab 有对应的内容版块,点击某个选项卡时,显示对应的内容版块,隐藏其他内容版块,并且为了突出当前的选项卡,还
制作四个选项卡页 Tab,用户可以通过切换不同的 Tab 页查看不同类别的新闻信息,每个 Tab 有对应的内容版块,点击某个选项卡时,显示对应的内容版块,隐藏其他内容版块,并且为了突出当前的选项卡,还
241 0
制作四个选项卡页 Tab,用户可以通过切换不同的 Tab 页查看不同类别的新闻信息,每个 Tab 有对应的内容版块,点击某个选项卡时,显示对应的内容版块,隐藏其他内容版块,并且为了突出当前的选项卡,还
tab栏切换制作(点击那一栏显示那一栏的内容,其他栏的内容隐藏)
tab栏切换制作(点击那一栏显示那一栏的内容,其他栏的内容隐藏)
tab栏切换制作(点击那一栏显示那一栏的内容,其他栏的内容隐藏)
|
定位技术 API
百度地图API禁用点击景点弹出详细信息的方法
百度地图API禁用点击景点弹出详细信息的方法