简体   繁体   English

使用jQuery将图像与另一个图像叠加(wordpress)

[英]overlay image with another image using jquery (wordpress)

I have following markup: 我有以下标记:

<a href="#"><img src="sample-article-img.jpg" alt="Mri Machine" title="medical" class="alignright size-medium wp-image-3109"></a>

I need to overlay this with another image (enlarge button): images/article-zoom.png 我需要用另一个图像(放大按钮)覆盖它: images/article-zoom.png

Problem #1, I cannot change markup. 问题1,我无法更改标记。 Problem #1, I don't have class or id on <a> 问题#1,我在<a>上没有课程或ID

My idea was to use jquery completely, drop zoom.png next to the first image (client side) put position absolute on it. 我的想法是完全使用jquery,将zoom.png放到第一张图片(客户端)旁边,将位置放在绝对位置。 But the problem here is that primary image will be floated, so my enlarge button will not move with it. 但是这里的问题是主要图像将浮动,因此我的放大按钮将不会随之移动。

Any ideas? 有任何想法吗?

You really haven't given a lot of detail to work with however as a starter adding a new image is fairly straighforward. 您确实没有提供很多细节要处理,但是作为入门者,添加新图像相当简单。

For demo will assume we are adding to any image with class that starts with wp-image . 对于演示,假设我们要添加任何以wp-image开头的类wp-image If only adding to a specific image will need more details about that image, and/or it's parent elements 如果仅添加到特定图像,则将需要有关该图像和/或其父元素的更多详细信息

Also need more detail about defining placement with regard to existing image. 还需要有关定义现有图像位置的更多详细信息。 I am assuming center over existing image. 我假设以现有图像为中心。

/* adjust accordingly*/
var buttonW = 120,/* button width*/
    buttonH = 40, /* button height*/
    $button = $('<img>', {src:  'drop zoom.png'});

var $image = $('img[class ^= "wp-image"]'),
    $image_link = $image.parent().css('position ', 'relative ');

var currImgPosition = $image.position(),
    currImgH = $image.height(),
    currImgW = $image.width();

var buttonCss = {
    position: 'absolute',
    zIndex : 10,
    width: buttonW,
    height: buttonH,
    top: currImgPosition.top + currImgH / 2 - buttonH / 2,
    left: currImgPosition.left + currImgW / 2 - buttonW / 2
}

$image_link.append( $button.css(buttonCss) );

EDIT: Some live html in jsfiddle.net with enough css to mimic existing elements would help also 编辑:jsfiddle.net中的一些实时HTML具有足够的CSS来模仿现有元素,这也将有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM