(function () { if (!$) { return; } function __$styleInject (css) { if (!css) return; if (typeof (window) == 'undefined') return; var style = document.createElement('style'); if ('styleSheet' in style) { style = document.createElement('div'); style.innerHTML = 'x'; style = style.lastChild; } else { style.innerHTML = css; } var head = document.getElementsByTagName('head')[0]; head.appendChild(style); return css; } function transformHtml ($dom, html) { var itemIdx = 0; var isMatch = 0; var imageList = []; var $children = $dom.children().each(function (idx, val) { var $val = $(val); $val.attr('nfc-image-post') && (isMatch += 1); imageList[itemIdx] = imageList[itemIdx] || {src: '', content: ''}; if (idx % 2) { imageList[itemIdx].content = $val.text(); itemIdx += 1; } else { var src = $val.find('img'); src = src ? src.attr('src') : ''; imageList[itemIdx].src = src.split('?')[0]; } }); if (isMatch !== $children.length) { return false; } return imageList; } function swiper ($dom, imageList) { var prev = $dom.find('.left.arrow')[0]; var next = $dom.find('.right.arrow')[0]; var list = $dom.find('.nfc-image-post-list')[0]; var $content = $dom.find('.nfc-image-post-item__descbox-content'); var $index = $dom.find('.nfc-image-post-item__descbox-index .index'); var index = 1; var animated = false; var width = parseFloat($dom.find('.nfc-image-post-container').width()); var limit = width * imageList.length; function animate (offset) { animated = true; var newLeft = parseInt(list.style.left) + offset; $(list).animate({ 'left': newLeft + 'px' }, 250, function () { animated = false; $content.text(imageList[index - 1].content); $index.html(index); if (newLeft > -width) { list.style.left = -limit + 'px'; } if (newLeft < -limit) { list.style.left = -width + 'px'; } }); } $(prev).off('click').on('click', function () { if (!animated) { if (index === 1) { index = imageList.length; } else { index -= 1; } animate(width); } }); $(next).off('click').on('click', function () { if (!animated) { if (index === imageList.length) { index = 1; } else { index += 1; } animate(-width); } }); } function createImagePostHtml (imageList, opt) { var imgHtml = []; $.each(imageList, function (key, val) { imgHtml.push( '
' + '
' + '' + '
' + '
'); }); var descHtml = '
' + '
1 / ' + imageList.length + '
' + '

' + imageList[0].content + '

' + '
'; var first = imgHtml[0]; var last = imgHtml[imgHtml.length - 1]; imgHtml.unshift(last); imgHtml.push(first); return '
' + imgHtml.join('') + '
' + descHtml; } window.__image_post_transformHtml = transformHtml; $.fn.extend({ imagePost: function (option) { var opt = $.extend({ width: $(this).width() }, option); var imageList = transformHtml(this); if (!imageList) { return; } var $that = this; function resetPosition (cb) { var html = createImagePostHtml(imageList, opt); this.empty(); this.html(html); this.addClass('nfc-image-post'); var min = Infinity; var idx = 0; function getMinHeight (val) { min = Math.min(min, $(val).height()); idx += 1; idx === (imageList.length + 2) && checkFuc(min); } this.find('.nfc-image-post-item img').each(function (key, val) { if (val.complete) { getMinHeight(val); } else { val.onload = function () { getMinHeight(val); }; } }); function checkFuc (min) { $that.find('.nfc-image-post-item img').each(function (key, val) { $(val).css('height', min + 'px'); }); $that.find('.arrow').css('top', min / 2 - 20 + 'px'); min = 0; $that.find('.nfc-image-post-item').each(function (key, val) { min = Math.max(min, $(val).height()); }); $that.find('.nfc-image-post-container').css('height', min + 'px'); } cb && cb.call(this); } resetPosition.call(this, function () { swiper(this, imageList); }); var timer = null; function handleResize () { timer && clearTimeout(timer) && (timer = null); timer = setTimeout(function () { if (!option.width) { opt.width = $that.width(); } resetPosition.call($that, function () { swiper(this, imageList); }); timer = null; }, 200); } if (!window.attachEvent) { $(window).on('resize', handleResize); } } }); __$styleInject('.nfc-image-post .arrow {\n' + ' font-size: 36px;\n' + ' font-weight: bold;\n' + ' line-height: 39px;\n' + ' position: absolute;\n' + ' z-index: 2;\n' + ' width: 40px;\n' + ' height: 40px;\n' + ' cursor: pointer;\n' + ' text-align: center;\n' + ' color: #fff;\n' + ' background: #B2B2B2;\n' + '}\n' + '.nfc-image-post .left {\n' + ' left: 20px;\n' + '}\n' + '.nfc-image-post .right {\n' + ' right: 20px;\n' + '}\n' + '.nfc-image-post-container {\n' + ' margin: 0 auto;\n' + ' position: relative;\n' + ' overflow: hidden;\n' + '}\n' + '.nfc-image-post-list {\n' + ' position: absolute;\n' + ' z-index: 1;\n' + ' top: 0;\n' + '}\n' + '.nfc-image-post-item {\n' + ' float: left;\n' + '}\n' + '.nfc-image-post-item__imagebox {\n' + ' width: 100%;\n' + ' text-align: center;\n' + '}\n' + '.nfc-image-post-item__imagebox img {\n' + ' max-width: 100%;\n' + '}\n' + '.nfc-image-post-item__descbox {\n' + ' box-sizing: border-box;\n' + ' padding-left: 80px;\n' + ' padding-right: 10px;\n' + ' min-height: 44px;\n' + ' position: relative;\n' + ' margin: 0 auto;\n' + '}\n' + '.nfc-image-post-item__descbox-content {\n' + ' box-sizing: border-box;\n' + ' padding-left: 10px;\n' + '}\n' + '.nfc-image-post-item__descbox-index {\n' + ' font-weight: bold;\n' + ' font-size: 28px;\n' + ' position: absolute;\n' + ' left: 0;\n' + ' top: 0;\n' + ' width: 80px;\n' + ' text-align: center;\n' + '}'); })();