使用页面加载按钮过滤图像 [英] Filter images with a button on page load

查看:21
本文介绍了使用页面加载按钮过滤图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力弄清楚如何在页面加载时过滤图像,而不仅仅是当用户使用 Shufflejs 单击 Bootstrap 中的按钮时.我希望选择水果"按钮以在页面加载时仅显示这些图像.当前,所有图像都显示在页面加载中.我试过使用checked"和active"属性,但它只检查按钮并仍然显示所有图像.

我是否缺少一个简单的解决方案?

代码如下:

var Shuffle = window.Shuffle;var myShuffle = new Shuffle(document.querySelector('.my-shuffle'), {itemSelector: '.image-item',sizer: '.my-sizer-element',缓冲区:1,});window.jQuery('input[name="shuffle-filter"]').on('change', function(evt) {var input = evt.currentTarget;如果(输入.检查){myShuffle.filter(input.value);}});

/* 默认样式,因此 shuffle 不必设置它们(如果它们丢失,它会设置)*/.my-shuffle {位置:相对;溢出:隐藏;}/* 使垂直槽与水平槽相同 */.image-item {底边距:30px;}/* 确保图片在加载时占用相同的空间 *//* https://vestride.github.io/Shuffle/images */.方面 {位置:相对;宽度:100%;高度:0;填充底部:100%;溢出:隐藏;}.aspect__内{位置:绝对;顶部:0;右:0;底部:0;左:0;}.aspect--16x9 {填充底部:56.25%;}.aspect--9x80 {padding-bottom: calc(112.5% + 30px);}.aspect--32x9 {填充底部:计算(28.125% - 8px);}.image-item img {显示:块;宽度:100%;最大宽度:无;高度:100%;适合对象:覆盖;}

<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1,shrink-to-fit=no"><meta http-equiv="x-ua-compatible" content="ie=edge"><!-- 以上 3 个元标记 * 必须* 排在首位;任何其他头部内容必须在*这些标签之后* --><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcFAgWiGinAmAmFcFAGORIGINSIGINX"><身体><div class="容器"><div class="container mt-3"><div class="row"><div class="col"><p class="mb-1">过滤器:</p>

<div class="row mb-3"><div class="col"><div class="btn-group" data-toggle="buttons"><label class="btn btn-outline-primary"><input type="radio" name="shuffle-filter" value="all" checked="checked"/>All<label class="btn btn-outline-primary"><input type="radio" name="shuffle-filter" value="nature"/>Nature<label class="btn btn-outline-primary.active"><input type="radio" name="shuffle-filter" value="fruit" 选中/>Fruit<label class="btn btn-outline-primary"><input type="radio" name="shuffle-filter" value="架构"/>架构

<div class="row my-shuffle"><figure class="image-item col-3" data-groups="[&quot;nature&quot;]"><div class="aspect aspect--16x9"><div class="aspect__inner"><img src="https://images.unsplash.com/uploads/141310026617203b5980d/c86b8baa?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=600&amp;h=338&amp;fit=crop&s=882e851a008e83b7a80d05bdc96aa817"/obj.tal"

</图><figure class="image-item col-3" data-groups="["架构&quot;]"><div class="aspect aspect--16x9"><div class="aspect__inner"><img src="https://images.unsplash.com/photo-1465414829459-d228b58caf6e?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=600&amp;h=338&amp;fit=crop&amp;s=7ab1744fe016fb39feb2972244246359="&obj"

</图><figure class="image-item col-3" data-groups="[&quot;nature&quot;,"architecture&quot;]"><div class="aspect aspect--9x80"><div class="aspect__inner"><img src="https://images.unsplash.com/photo-1416184008836-5486f3e2cf58?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=601&amp;h=676&amp;fit=crop&amp;s=5f1f1ffba05979d4248cc16d8eb82f0a=".obj.alt"

</图><figure class="image-item col-3" data-groups="["fruit&quot;]"><div class="aspect aspect--16x9"><div class="aspect__inner"><img src="https://images.unsplash.com/photo-1464454709131-ffd692591ee5?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=600&amp;h=338&amp;fit=crop&amp;s=eda14f45e94e9024f854b1e06708c629"/obj.tal

</图><div class="col-1 my-sizer-element"></div>

这是一个带有代码的代码笔:https://codepen.io/anon/pen/KoZQEN

解决方案

$(document).ready(function() {myShuffle.filter('水果');});

当您的文档准备好加载时,这应该选择水果.在 javascript 文件的开头添加这段代码.

I'm struggling to figure out how to have images filtered when the page loads, not just when the user clicks on the buttons in Bootstrap using Shufflejs. I'm wanting the "Fruit" button selected to show only those images when the page loads. Currently all the images show on page load. I've tried using "checked" and using the "active" attribute, but it only checks the button and still shows all of the images.

Am I missing a simple solution to this?

Here's the code:

var Shuffle = window.Shuffle;

var myShuffle = new Shuffle(document.querySelector('.my-shuffle'), {
  itemSelector: '.image-item',
  sizer: '.my-sizer-element',
  buffer: 1,
});

window.jQuery('input[name="shuffle-filter"]').on('change', function(evt) {
  var input = evt.currentTarget;
  if (input.checked) {
    myShuffle.filter(input.value);
  }
});

/* default styles so shuffle doesn't have to set them (it will if they're missing) */

.my-shuffle {
  position: relative;
  overflow: hidden;
}


/* Make vertical gutters the same as the horizontal ones */

.image-item {
  margin-bottom: 30px;
}


/* Ensure images take up the same space when they load */


/* https://vestride.github.io/Shuffle/images */

.aspect {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 100%;
  overflow: hidden;
}

.aspect__inner {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.aspect--16x9 {
  padding-bottom: 56.25%;
}

.aspect--9x80 {
  padding-bottom: calc(112.5% + 30px);
}

.aspect--32x9 {
  padding-bottom: calc(28.125% - 8px);
}

.image-item img {
  display: block;
  width: 100%;
  max-width: none;
  height: 100%;
  object-fit: cover;
}

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

</head>

<body>




  <div class="container">



    <div class="container mt-3">
      <div class="row">
        <div class="col">
          <p class="mb-1">Filters:</p>
        </div>
      </div>
      <div class="row mb-3">
        <div class="col">
          <div class="btn-group" data-toggle="buttons">
            <label class="btn btn-outline-primary">
          <input type="radio" name="shuffle-filter" value="all" checked="checked"/>All
        </label>
            <label class="btn btn-outline-primary">
          <input type="radio" name="shuffle-filter" value="nature"/>Nature
        </label>
            <label class="btn btn-outline-primary.active">
          <input type="radio" name="shuffle-filter" value="fruit" checked/>Fruit
        </label>
            <label class="btn btn-outline-primary">
          <input type="radio" name="shuffle-filter" value="architecture"/>Architecture
        </label>
          </div>
        </div>
      </div>
      <div class="row my-shuffle">
        <figure class="image-item col-3" data-groups="[&quot;nature&quot;]">
          <div class="aspect aspect--16x9">
            <div class="aspect__inner"><img src="https://images.unsplash.com/uploads/141310026617203b5980d/c86b8baa?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=600&amp;h=338&amp;fit=crop&amp;s=882e851a008e83b7a80d05bdc96aa817" obj.alt="obj.alt" /></div>
          </div>
        </figure>
        <figure class="image-item col-3" data-groups="[&quot;architecture&quot;]">
          <div class="aspect aspect--16x9">
            <div class="aspect__inner"><img src="https://images.unsplash.com/photo-1465414829459-d228b58caf6e?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=600&amp;h=338&amp;fit=crop&amp;s=7ab1744fe016fb39feb2972244246359" obj.alt="obj.alt" /></div>
          </div>
        </figure>
        <figure class="image-item col-3" data-groups="[&quot;nature&quot;,&quot;architecture&quot;]">
          <div class="aspect aspect--9x80">
            <div class="aspect__inner"><img src="https://images.unsplash.com/photo-1416184008836-5486f3e2cf58?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=601&amp;h=676&amp;fit=crop&amp;s=5f1f1ffba05979d4248cc16d8eb82f0a" obj.alt="obj.alt" /></div>
          </div>
        </figure>
        <figure class="image-item col-3" data-groups="[&quot;fruit&quot;]">
          <div class="aspect aspect--16x9">
            <div class="aspect__inner"><img src="https://images.unsplash.com/photo-1464454709131-ffd692591ee5?ixlib=rb-0.3.5&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=600&amp;h=338&amp;fit=crop&amp;s=eda14f45e94e9024f854b1e06708c629" obj.alt="obj.alt" /></div>
          </div>
        </figure>
        <div class="col-1 my-sizer-element"></div>
      </div>
    </div>






  </div>
</body>

Here's a codepen with the code as well: https://codepen.io/anon/pen/KoZQEN

解决方案

$(document).ready(function() { 
    myShuffle.filter('fruit');
});

This should select fruits when your document is ready with loading. Add this piece of code at the start of your javascript file.

这篇关于使用页面加载按钮过滤图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆