Created
November 24, 2017 03:29
-
-
Save 337547038/a965729b25e37892fe6594ac77272f71 to your computer and use it in GitHub Desktop.
jquery图片垂直水平居中
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function ($) { | |
$.fn.extend({ | |
imgHook: function (opt) { | |
opt = jQuery.extend({className: "img-hook"}, opt); | |
var th = $(this); | |
if (th.length > 0) { | |
if (th[0].tagName == 'IMG') { | |
th.wrap('<div class="' + opt.className + '"></div>'); | |
th.before('<b class="hook"></b>') | |
} else { | |
th.addClass(opt.className); | |
th.prepend('<b class="hook"></b>') | |
} | |
} | |
} | |
}); | |
})(jQuery); | |
//引用 | |
$(".img").imgHook(); | |
/* | |
主要样式 | |
.img-hook { text-align: center; font-size: 0; overflow: hidden } | |
.img-hook img { vertical-align: middle; max-width: 100%; max-height: 100% } | |
.img-hook .hook { display: inline-block; width: 0; height: 100%; overflow: hidden; font-size: 0; line-height: 0; vertical-align: middle } | |
*/ | |
/* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment