Skip to content

Instantly share code, notes, and snippets.

@hnq90
Created May 22, 2015 02:15
Show Gist options
  • Select an option

  • Save hnq90/9b6fdf27c677f8b71313 to your computer and use it in GitHub Desktop.

Select an option

Save hnq90/9b6fdf27c677f8b71313 to your computer and use it in GitHub Desktop.
Images Protector
$.fn.protectImage = (settings) ->
settings = jQuery.extend(
zIndex: 10
offset: 0
, settings)
@each ->
thisImg = $(this)
position = thisImg.offset()
height = thisImg.height()
width = thisImg.width()
srcimg = this
protect = $("<div>")
protect.css({
width: width
height: height
top: position.top
left: position.left - settings.offset
position: "absolute"
zIndex: settings.zIndex
})
.addClass("image-protector")
if $(srcimg).parent().get(0).tagName is "A"
link = $(srcimg).parent().clone()
link.html protect
link.appendTo thisImg.parent()
else
$(protect).appendTo thisImg.parent()
$.fn.protectImage2 = (settings) ->
settings = jQuery.extend(
zIndex: 10
, settings)
@each ->
thisImg = $(this)
height = thisImg.height()
width = thisImg.width()
title = thisImg.attr("alt")
currentStyle = thisImg.attr("style")
thisImg.wrap "<div></div>"
.parent()
.css({
display: "block"
width: width
height: height
background: "url(" + thisImg.attr("src") + ") no-repeat"
zIndex: settings.zIndex
})
.addClass thisImg.attr("class")
.addClass "image-protector"
.removeClass "protect"
.attr "title", title
if currentStyle && currentStyle.length > 0
newStyle = thisImg.parent().attr("style");
thisImg.parent().attr("style", newStyle + currentStyle)
thisImg.css display: "none"
$.fn.reloadProtector = (settings) ->
settings = jQuery.extend(
offset: 0
, settings)
selector = $(this)
$(".image-protector").remove()
$(selector).protectImage({offset: settings.offset})
$.fn.reloadProtector2 = ->
@each ->
selector = $(this)
selector.parent().replaceWith ->
$ "img", this
selector.protectImage2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment