首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >元素数组中的Enquire.js (javascript)

元素数组中的Enquire.js (javascript)
EN

Stack Overflow用户
提问于 2016-01-23 11:24:40
回答 1查看 39关注 0票数 1

所讨论的代码如下:https://jsfiddle.net/ozeasa8t/

目标是通过缓存元素数组对每个()进行访问,以便在enquire.js驱动的js中创建切换效果。

我得到了错误左和右,并会喜欢你的意见。谢谢!

代码语言:javascript
复制
// I'm separating block A and B in declaration because the cached elements are used for other things
var $blockA = $('.block-a'),
		$blockB = $('.block-b');

// Creating array
var blockArray = [$blockA,$blockB];

var handler = function(el) {
  el.find('.block-content').toggle();
  el.closest('.wrapper').toggleClass('open');
};

blockArray.each(function() {
  enquire.register('screen and (max-width:1023px)', {
    match: function() {
    
      // Wrap each block in a .wrapper 
      $(this).wrap('<div class="wrapper"></div>');
      
      // .block-content should be hidden initially
      $(this).find('.block-content').hide();
	    
      // .block-title will toggle its sibling .block-content
      $(this).find('.block-title').bind( "click", handler($(this)));

    },
    unmatch: function() {
    
      // Unwrap .wrapper
      $(this).unwrap();

      // Unbind so we don't end up with toggling block in desktop
      $(this).find('.block-title').unbind( "click", handler($(this)));
    }
  })
});
代码语言:javascript
复制
.block {
  margin:20px;
  background:#fff;
  border:1px solid black; 
  font:14px sans-serif;
  }
.block .block-title {
  background:#f4f4f4;
  padding:10px 15px;
  font-weight:bold;
  }
.block .block-content {
  padding:10px 15px;
  }
代码语言:javascript
复制
<div class="block-a block">
   <div class="block-title">
     Block A TItle
   </div>
   <div class="block-content">
     Bluh Bluh
   </div>
</div>
<div class="block-b block">
   <div class="block-title">
     Block B TItle
   </div>
   <div class="block-content">
     Bluh Bluh
   </div>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-24 09:26:08

以下是工作的js更新。

代码语言:javascript
复制
var handler = function($el) {
  $el.find('.block-content').toggle();
  $el.closest('.wrapper').toggleClass('open');
};e

$('.block').each(function() {
  var $el = $(this);
  enquire.register('screen and (min-width:768px)', {
    match: function() {
      $el.wrap('<div class="wrapper"></div>');
      $el.find('.block-content').hide();
      $el.find('.block-title').bind( "click", function() { handler($el); });

    },
    unmatch: function() {
      $el.unwrap();
      $el.find('.block-title').unbind( "click");
      $el.find('.block-content').show();
      $el.closest('.wrapper').removeClass('open')
    }
  })
});

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34962825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档