原生js实现的带平滑动画展开收缩特效代码

所属分类: 网页特效-展开&收缩    2023-11-26 04:31:53

原生js实现的带平滑动画展开收缩特效代码 ie兼容6
反馈问题  查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

原生js实现的带平滑动画展开收缩特效代码(共3个文件)

    • index.html

使用方法

"use strict";

//we want to get all blockquotes as an array instead of a nodelist
const blockquotes = [].slice.call(document.querySelectorAll('blockquote'));

//do the things we need to to each blockquote on the page
blockquotes.map(blockquote => {
  //we only want an expander to be added if it is taller than 200px
  if(blockquote.clientHeight > 200) {
    //add data-state closed to the blockquote
    blockquote.setAttribute('data-state','closed');
    //create a span that will hold our expand button
    const gradient = document.createElement("span");
    //give it the class .bqe-gradient to add a gradient background
    gradient.classList.add('bqe-gradient');
    
    //create the expander button
    const expander = document.createElement("span");
    //add .bqe-expander class for styling
    expander.classList.add('bqe-expander');
    //add a funky hand pointer
    expander.innerHTML = '☟';  
    //add our expander element into the gradient element and then
    //add the gradient element into the blockquote
    blockquote.appendChild(gradient).appendChild(expander);
  }
});

//get all the .bqe-expander elements as array
const allExpanders = [].slice.call(document.querySelectorAll('.bqe-expander'));
//add event listener to each element so we can toggle the data-state 
allExpanders.map(expander => {expander.addEventListener('click', expandBlockquote)});

function expandBlockquote() {
  //our button is nested in the gradient span
  //so we want the parent of the parent
  const parEl = this.parentElement.parentElement;
  //toggle our data state thanks to:
  //https://toddmotto.com/stop-toggling-classes-with-js-use-behaviour-driven-dom-manipulation-with-data-states/
  parEl.setAttribute('data-state', parEl.getAttribute('data-state') === 'open' ? 'closed' : 'open');
}

站长提示:
1. 平台上所有素材资源,需注册登录会员方能正常下载。
2. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励20K币
3. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
4. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 展开&收缩

jquery可折叠选中的树结构展开收缩插件

一款支持多选的树结构导航折叠菜单,支持无限级父子菜单,超实用。
  展开&收缩
 152  

jquery折叠展开更多文本内容插件

一款“显示更多”展开收缩特效,可让DIV中的文本文字内容实现展开和折叠功能。
  展开&收缩
 293  

jquery支持屏蔽禁用的手风琴展开收缩插件

一款常用的问答显示隐藏插件,支持对指定div进行禁止点击事件,禁止触发展开收缩特效。
  展开&收缩
 158  

jquery手机排行榜标题图片滑动展开代码

一款侧边栏鼠标hover触发显示隐藏特效,默认显示一个。
  展开&收缩
 132  

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论