functions - Images with overlay

I have some images in a container that I would like to add an overlay and an icon. This doesn't work out of the box

I have some images in a container that I would like to add an overlay and an icon. This doesn't work out of the box, but I found some code that helped:

HTML:

<div class="main">
    <span class="featured"><img src=".jpg" title="" alt=""></span>
</div>


CSS:

.featured {
    display: inline-block;
    margin: 30px 20px 0;
    position: relative;
}

.featured img {
    vertical-align: top;
    width: 100%;
}

.featured:hover {
    cursor: pointer;
}

.featured:after {
    background: rgba(0,0,0,0.5);
    border-radius: 50%;
    content: '\A';
    height: 100%;
    left: 0;
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    /* Transition */
    transition: all 0.5s;
    -webkit-transition: all 0.5s;
}

.featured:hover:after {
    color: #fff;
    content: '\f2f5';
    font-family: "Ionicons";
    font-size: 20px;
    opacity: 1;
}

jQuery:

$(document).ready(function(){
    var img = $(".featured > img");
    $(".featured").css({width:img.width(), height:img.height()});
});

I like the code because it's simple and it works on the images regardless of their dimensions.

<span class="featured"><img class="alignright size-medium wp-image-75" src=".jpg" alt="" width="300" height="225"></span>


Unfortunately, if I have post images that have been specifically aligned, this code will break that...is there any way to do the same thing while preserving the alignright, alignleft, or aligncenter class on the images?

Thanks,
Josh

I have some images in a container that I would like to add an overlay and an icon. This doesn't work out of the box, but I found some code that helped:

HTML:

<div class="main">
    <span class="featured"><img src="http://joshrodg/IMG_001-258x258.jpg" title="" alt=""></span>
</div>


CSS:

.featured {
    display: inline-block;
    margin: 30px 20px 0;
    position: relative;
}

.featured img {
    vertical-align: top;
    width: 100%;
}

.featured:hover {
    cursor: pointer;
}

.featured:after {
    background: rgba(0,0,0,0.5);
    border-radius: 50%;
    content: '\A';
    height: 100%;
    left: 0;
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    /* Transition */
    transition: all 0.5s;
    -webkit-transition: all 0.5s;
}

.featured:hover:after {
    color: #fff;
    content: '\f2f5';
    font-family: "Ionicons";
    font-size: 20px;
    opacity: 1;
}

jQuery:

$(document).ready(function(){
    var img = $(".featured > img");
    $(".featured").css({width:img.width(), height:img.height()});
});

I like the code because it's simple and it works on the images regardless of their dimensions.

<span class="featured"><img class="alignright size-medium wp-image-75" src="http://joshrodg/wp-content/uploads/IMG_001-300x225.jpg" alt="" width="300" height="225"></span>


Unfortunately, if I have post images that have been specifically aligned, this code will break that...is there any way to do the same thing while preserving the alignright, alignleft, or aligncenter class on the images?

Thanks,
Josh

Share Improve this question asked Mar 28, 2019 at 16:07 joshmrodgjoshmrodg 1,1731 gold badge16 silver badges42 bronze badges 2
  • How are you adding this code (the wrapping div and span) to images that are inserted in the WordPress editor now? What happens when alignment is applied? – Michelle Commented Mar 28, 2019 at 21:35
  • So, the div class is just my container...that's not around every single image. I'm sorry, I didn't mention that. The span is being applied via a WP function from the accepted answer - wordpress.stackexchange/questions/36000/… (except instead of the div class "my photo", I'm using the span class "featured") – joshmrodg Commented Mar 29, 2019 at 12:25
Add a comment  | 

1 Answer 1

Reset to default 0

I was able to find some jQuery that helped solve the issue:

$( ".alignright" ).parent().addClass( "alignright" );
$( ".alignleft" ).parent().addClass( "alignleft" );
$( ".aligncenter" ).parent().addClass( "aligncenter" );

This small piece of code finds alignright, alignleft, and aligncenter then adds those classes to the "parent" container, which is the containing element that those classes are applied to - in my case that would be the "featured" span.

Thanks,
Josh

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745646788a4638016.html

相关推荐

  • functions - Images with overlay

    I have some images in a container that I would like to add an overlay and an icon. This doesn't work out of the box

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信