flagstone.js是一款强大的自适应可拖拽排序的瀑布流插件

flagstone.js是一款强大的自适应可拖拽排序的瀑布流插件

flagstone.js是一款强大的自适应可拖拽排序的瀑布流插件,兼容手机端,元素可以进行自由的拖拽排序,并伴随有排序动画效果。

使用方式:

第一步引入<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/flagstone-4.0.0.min.js"></script>

第二步配置参数

const fs = flagstone(document.getElementsByTagName('article')[0], {
  //bedPadding: 10, // The padding around the area edge
  //stonesMargin: 10, // Margin between stones in pixels (as an int)
  margin: 10, //间距
  minWidth: 280, // 最小宽度像素
  maxColumns: 5, // 最大列数
  flow: true, //当浏览器视窗改变重新排列动画是否开启
  direction: 'left', // 显示方向,默认从左边开始,也可以选择right从右边开始
  assignReverseZIndexes: false,
  random: false, // 是否随机显示
  square: false, // Makes each flagstone square; default is false
  space: false, // Spaces out stones randomly (as a boolean)
  spaceFrequency: 0.4, // Adjusts the frequency of the amount of spaces (as a float 0.0 - 1.0); default is 0.4
  animationDuration: 1000, // 动画时间秒
  animationTimingFunction: 'ease', // 过渡动画缓冲
  heightAnimationDuration: 2500,
  heightAnimationTimingFunction: 'ease',
  resizeDelay: 250, // 重新排列延迟毫秒
  dragAndDrop: true,
  dragAndDropAutoDelay: 1000,
  dropCallback: function (dragElem, targetElem) {
    // console.log(dragElem);
    // console.log(targetElem);
    return true;
  },
  eventResetDelay: 250,
  eventResizeHeightDuration: 250,
  callback: function (elem, index) {// 大小重新绘制resize事件监听函数
    if (index === 3) {
      elem.classList.add('highlight');
    } else {
      elem.classList.remove('highlight');
    }
  }, // Callback function for each element with index and element arguments
  watch: true,
  watchAll: true,
  watchImages: true });

好了,我们来看看一个示例

<!DOCTYPE html>
<html lang="en" >

<head>

  <meta charset="UTF-8">

  
  
  
<style>
@import url("https://fonts.googleapis.com/css?family=Open+Sans");
*, *:before, *:after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  background: #777;
}

body {
  font: 400 18px/1.4 "Open Sans", sans-serif;
  margin: 0 2vw;
  background: inherit;
  min-height: 1100px;
}

h1 {
  margin: 10px;
  padding-top: 30px;
  font-size: 48px;
  color: #aaa;
  text-shadow: 0 0 5px #5d646e;
  text-align: center;
}

h1 a {
  text-decoration: none;
  color: inherit;
}

.slogan {
  display: block;
  font-size: 10px;
  text-shadow: initial;
  text-transform: uppercase;
  letter-spacing: 8px;
  text-indent: 8px;
}

.quick-info {
  display: block;
  margin: 10px 0;
  font-family: sans-serif;
  font-size: 11px;
  text-shadow: initial;
  text-align: center;
  text-transform: uppercase;
  color: #aaa;
}

button {
  border: none;
  outline: none;
}

p {
  margin-top: 1em;
}

.adjust {
  position: fixed;
  z-index: 100;
  top: -290px;
  left: 0;
  width: 350px;
  height: 290px;
  border-radius: 0 0 10px 0;
  padding: 10px 10px 0 10px;
  background-color: #aaa;
  color: #fff;
  font-size: 12px;
  transition: top 0.25s ease;
  box-shadow: 4px 4px 4px 0 #5d646e;
}
.adjust.active {
  top: 0px;
}
.adjust .toggle-widget {
  padding-top: 10px;
  margin-top: 10px;
  font-size: 16px;
  color: #ff0;
  cursor: pointer;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
}
.adjust .toggle-widget:after {
  content: "";
  display: table;
  clear: both;
}
.adjust .toggle-widget p {
  float: left;
  margin: 0;
}
.adjust .toggle-widget button {
  float: right;
  background: none;
  line-height: 1em;
  font-size: 18px;
  color: inherit;
}
.adjust table {
  table-layout: fixed;
  border-collapse: collapse;
}
.adjust th {
  border-bottom: 1px solid #fff;
  padding-bottom: 4px;
  font-weight: 800;
}
.adjust th:first-of-type {
  width: 50%;
}
.adjust td {
  padding-top: 4px;
}
.adjust tfoot button {
  padding: 5px;
  background: #ff0;
  color: #5d646e;
  cursor: pointer;
}
.adjust tfoot button:focus {
  box-shadow: 2px 2px 4px 0 #000;
}
.adjust input,
.adjust textarea {
  color: #5d646e;
}
.adjust textarea {
  width: 100%;
  height: 33px;
}

.add-card {
  margin-bottom: 10px;
  padding: 5px;
  background: #ff0;
  color: #5d646e;
  cursor: pointer;
}
.add-card:focus {
  box-shadow: 2px 2px 4px 0 #000;
}

article {
  margin-bottom: 100px;
  background: #aaa;
}

article section {
  overflow-y: auto;
  padding: 15px;
  background-image: linear-gradient(to bottom right, #5d646e, #868080);
  color: #fff;
}

img {
  width: 100%;
}

iframe {
  width: 100%;
  height: 56.25%;
}

.p-animate {
  overflow: hidden;
  max-height: 40px;
  transition: max-height 0.25s ease;
}
.p-animate.active {
  max-height: 200px;
}
.p-animate.active + .show-more {
  display: none;
}
.p-animate.active + .show-more + .show-less {
  display: inline-block;
}
.p-animate + .show-more {
  display: inline-block;
}
.p-animate + .show-more + .show-less {
  display: none;
}

.toggle-more {
  padding: 5px;
  background: #ff0;
  color: #5d646e;
  cursor: pointer;
}
.toggle-more:focus {
  box-shadow: 2px 2px 4px 0 #000;
}

.highlight {
  background: #ff0;
  color: #5d646e;
}

.flagstone-remove {
  position: absolute;
  top: 0;
  right: 10px;
  margin: 0;
  opacity: 0.25;
  padding: 0;
  background: none;
  cursor: pointer;
  font-size: 24px;
}
.flagstone-remove:focus, .flagstone-remove:hover {
  opacity: 1;
}

.flagstone-0-bed.flagstone-dragover {
  box-shadow: inset 0 0 4px 4px #ff0;
}

.flagstone-drag {
  filter: grayscale(1) blur(4px);
  opacity: 0.25;
}

.flagstone-left {
  box-shadow: -8px 0 4px -4px #ff0;
}

.flagstone-right {
  box-shadow: 8px 0 4px -4px #ff0;
}
</style>



</head>

<body  >
 

<div class="adjust">
  <table>
    <thead>
      <tr>
        <th>Property</th>
        <th>Value</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>margin:</td>
        <td><input type="number" min="0" max="1000" disabled id="adjust-margin"></td>
      </tr>
      <tr>
        <td>bedPadding:</td>
        <td><input type="number" min="0" max="1000" value="10" id="adjust-bed-padding"></td>
      </tr>
      <tr>
        <td>stonesMargin:</td>
        <td><input type="number" min="0" max="1000" value="10" id="adjust-stones-margin"></td>
      </tr>
      <tr>
        <td>minWidth:</td>
        <td><input type="number" min="0" max="1000" value="280" id="adjust-min-width"></td>
      </tr>
      <tr>
        <td>maxColumns:</td><td><input type="number" min="0" max="1000" value="5" id="adjust-max-columns"></td>
      </tr>
      <tr>
        <td>dragAndDrop:</td>
        <td><input type="checkbox" checked id="adjust-drag-and-drop"></td>
      </tr>
      <tr>
        <td>dropCallback:</td>
        <td><textarea id="adjust-drop-callback">function(dragElem, targetElem) {
    return true;
  }</textarea></td>
      </tr>
      <tr>
        <td>callback:</td>
        <td><textarea id="adjust-callback">function(elem, index) {
    if (index === 3) {
      elem.classList.add('highlight');
    } else {
      elem.classList.remove('highlight');
    }
  }</textarea></td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <td><button class="apply-adjustments">Apply</button></td>
        <td></td>
      </tr>
    </tfoot>
  </table>
  <div class="toggle-widget">
    <p>Toggle Adjust Widget</p>
    <button>⇕</button>
  </div>
</div>

<button class="add-card">Inject a card</button>

<article style="visibility:hidden;">
  <section class="flagstone-drag-handle">
    <h2>1</h2>
    <button class="flagstone-remove">×</button>
    <p class="p-animate">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ducimus unde amet quam omnis aliquid aliquam culpa, explicabo ut officia illum doloremque debitis, fugiat eos. Hic cupiditate sapiente placeat temporibus.</p>
    <button class="toggle-more show-more flagstone-resize-height">Show More</button>
    <button class="toggle-more show-less flagstone-resize-height">Show Less</button>
  </section>
  <section class="flagstone-drag-handle">
    <h2>2</h2>
    <button class="flagstone-remove">×</button>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ducimus unde amet quam omnis aliquid aliquam culpa, explicabo ut officia illum doloremque debitis, fugiat eos. Hic cupiditate sapiente placeat temporibus.</p>
    <img src="//repo.bfw.wiki/bfwrepo/image/5e62ef20b92ee.png" alt="" style="float:left;width:80px;margin:10px 10px 10px 0;"/>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ducimus unde amet quam omnis aliquid aliquam culpa, explicabo ut officia illum doloremque debitis, fugiat eos. Hic cupiditate sapiente placeat temporibus.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ducimus unde amet quam omnis aliquid aliquam culpa, explicabo ut officia illum doloremque debitis, fugiat eos. Hic cupiditate sapiente placeat temporibus.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ducimus unde amet quam omnis aliquid aliquam culpa, explicabo ut officia illum doloremque debitis, fugiat eos. Hic cupiditate sapiente placeat temporibus.</p>
  </section>
  <section class="flagstone-drag-handle">
    <h2>3</h2>
    <button class="flagstone-remove">×</button>
    <img src="//repo.bfw.wiki/bfwrepo/image/5e62ef20b92ee.png" alt="" id="change-my-source"/>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
  </section>
  <section class="flagstone-drag-handle">
    <h2>4</h2>
    <button class="flagstone-remove">×</button>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ducimus unde amet quam omnis aliquid aliquam culpa, explicabo ut officia illum doloremque debitis, fugiat eos. Lorem ipsum dolor sit amet hic cupiditate sapiente placeat temporibus.</p>
  </section>
  <section class="flagstone-drag-handle">
    <h2>5</h2>
    <button class="flagstone-remove">×</button>
    <p>Lorem ipsum dolor sit amet. Hic cupiditate sapiente placeat temporibus, placeat temporibus, fugiat eos.</p>
  </section>
  <section class="flagstone-drag-handle">
    <h2>6</h2>
    <button class="flagstone-remove">×</button>
    <p>Lorem ipsum dolor sit amet. Accusamus ducimus unde amet quam omnis aliquid aliquam culpa, explicabo ut officia illum doloremque debitis, fugiat eos. Hic cupiditate sapiente placeat temporibus.</p>
  </section>
  <section class="flagstone-drag-handle">
    <h2>7</h2>
    <button class="flagstone-remove">×</button>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ducimus unde amet quam omnis aliquid aliquam culpa, explicabo ut officia illum doloremque debitis, fugiat eos. Hic cupiditate sapiente placeat temporibus. Accusamus ducimus unde amet quam omnis.</p>
  </section>
  <section class="flagstone-drag-handle">
    <h2>8</h2>
    <button class="flagstone-remove">×</button>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ducimus unde amet quam omnis aliquid aliquam culpa, explicabo ut officia illum doloremque debitis, fugiat eos. Hic cupiditate sapiente placeat temporibus. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ullam quibusdam placeat dolorum natus voluptatum reiciendis cupiditate, cum veritatis accusamus labore consectetur ab amet, sit commodi laudantium beatae molestiae iste repellendus.</p>
  </section>
  <section class="flagstone-drag-handle">
    <h2>9</h2>
    <button class="flagstone-remove">×</button>
    <p>Lorem ipsum dolor sit amet.</p>
  </section>
  <section class="flagstone-drag-handle">
    <h2>10</h2>
    <button class="flagstone-remove">×</button>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ducimus unde amet quam omnis aliquid aliquam culpa, explicabo ut officia illum doloremque debitis, fugiat eos. Hic cupiditate sapiente placeat temporibus.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ducimus unde amet quam omnis aliquid aliquam culpa, explicabo ut officia illum doloremque debitis, fugiat eos. Hic cupiditate sapiente placeat temporibus.</p>
  </section>
  <section class="flagstone-drag-handle">
    <h2>11</h2>
    <button class="flagstone-remove">×</button>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus.</p>
  </section>
</article>



<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/flagstone-4.0.0.min.js"></script>



      <script>
// Full documentation: https://github.com/depthdev/flagstonejs

const fs = flagstone(document.getElementsByTagName('article')[0], {
  //bedPadding: 10, // The padding around the area edge
  //stonesMargin: 10, // Margin between stones in pixels (as an int)
  margin: 10, // This overrides bedPadding & stonesMargin and is equivalent to setting them both to this value
  minWidth: 280, // Minimum width you want an element to be in pixels (as an int)
  maxColumns: 5, // Maximum number of columns to display (as an int)
  flow: true, // Allows stone widths to flow during a resize event
  direction: 'left', // Alternative is "right"; the default is "left"; and for top to bottom, use CSS's built in Columns or Flexbox
  assignReverseZIndexes: false,
  random: false, // Display stones in a random order; the default is false (as a boolean)
  square: false, // Makes each flagstone square; default is false
  space: false, // Spaces out stones randomly (as a boolean)
  spaceFrequency: 0.4, // Adjusts the frequency of the amount of spaces (as a float 0.0 - 1.0); default is 0.4
  animationDuration: 1000, // Animation duration (milliseconds as an int); default is 0
  animationTimingFunction: 'ease', // CSS animation timing function as a string
  heightAnimationDuration: 2500,
  heightAnimationTimingFunction: 'ease',
  resizeDelay: 250, // Delay to run resize/reset function after resizing the window in milliseconds (as an int); default is 250
  dragAndDrop: true,
  dragAndDropAutoDelay: 1000,
  dropCallback: function (dragElem, targetElem) {
    // console.log(dragElem);
    // console.log(targetElem);
    return true;
  },
  eventResetDelay: 250,
  eventResizeHeightDuration: 250,
  callback: function (elem, index) {// Warning! This gets called every soft reset, so if you're attaching listeners, you'll need to remove the listeners first!
    if (index === 3) {
      elem.classList.add('highlight');
    } else {
      elem.classList.remove('highlight');
    }
  }, // Callback function for each element with index and element arguments
  watch: true,
  watchAll: true,
  watchImages: true });


function adjust() {
  const $ = function (s) {return document.getElementById(s);};
  fs.adjust({
    //margin: 10,
    bedPadding: window.parseInt($('adjust-bed-padding').value, 10),
    stonesMargin: window.parseInt($('adjust-stones-margin').value, 10),
    minWidth: window.parseInt($('adjust-min-width').value, 10),
    maxColumns: window.parseInt($('adjust-max-columns').value, 10),
    dragAndDrop: $('adjust-drag-and-drop').checked,
    dropCallback: window.eval('(' + $('adjust-drop-callback').value + ')'),
    callback: window.eval('(' + $('adjust-callback').value + ')') });

}

function toggleMore(e) {
  const p = e.target.parentElement.getElementsByClassName('p-animate')[0];
  if (p.classList.contains('active')) {
    p.classList.remove('active');
  } else {
    p.classList.add('active');
  }
}

function toggleWidget(e) {
  const container = e.currentTarget.parentElement;
  if (container.classList.contains('active')) {
    container.classList.remove('active');
  } else {
    container.classList.add('active');
  }
}

function addCard() {
  const section = document.createElement('section');
  section.classList.add('flagstone-drag-handle');
  section.innerHTML = '<h2>New Card</h2><button class="flagstone-remove">×</button><p>Lorem ipsum dolor sit amet. Hic cupiditate sapiente placeat temporibus, placeat temporibus, fugiat eos.</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Velit tempora itaque maxime, enim, nam quas nobis voluptate, minus ex adipisci aperiam provident sequi soluta. Praesentium et, placeat porro dolorum voluptate.</p>';
  document.getElementsByTagName('article')[0].appendChild(section);
}

Array.prototype.slice.call(document.getElementsByClassName('toggle-more')).forEach(function (e) {
  e.addEventListener('click', toggleMore);
});

document.getElementsByClassName('toggle-widget')[0].addEventListener('click', toggleWidget);

document.getElementsByClassName('apply-adjustments')[0].addEventListener('click', adjust);

document.getElementsByClassName('add-card')[0].addEventListener('click', addCard);

window.setTimeout(function () {
  document.getElementById('change-my-source').src = '//repo.bfw.wiki/bfwrepo/image/5e62ef20b92ee.png';
}, 3000);

    </script>

  

</body>

</html>
 


立即下载flagstone-4.0.0.min.js查看所有js插件

网友评论0

程序员在线工具箱