简体   繁体   English

CSS-HTML:使用 html 和 css 将图像放在另一个图像之上

[英]CSS-HTML: Put an image above another one with html and css

I'm very newbie to webdev, but I need to draft a landingpage with some trick effects.我是 webdev 的新手,但我需要起草一个带有一些技巧效果的登录页面。 I need to put a "stencil image" (png with transp) over a moving (mouse hover) background.我需要在移动(鼠标悬停)背景上放置一个“模板图像”(带有 transp 的 png)。 Basically, I managed to do that, but I having a big issue: If I resize browser, the background shows behind first plane image, (because I can't rezise background to browser size).基本上,我设法做到了,但我遇到了一个大问题:如果我调整浏览器的大小,背景会显示在第一个平面图像的后面,(因为我无法将背景调整为浏览器大小)。 So, the main codes are:所以,主要代码是:

html code: html 代码:

<div id="bkg-image" class="blur"></div> 

<div>
<img  src="./imgs/stencil.png" class="firstPlane" />

</div>

CSS code: CSS 代码:

#bkg-image {
    background: url('./imgs/background.jpg') no-repeat center center fixed;
    position: absolute;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    width: 75%;
    height: 75%;
    z-index: 0;
    -webkit-filter: brightness(1.7);
}

.firstPlane {
    display: block;
    position: absolute;
    top: 0px;
    left: 0px;
    max-width: 100%;
    height: auto;
    width: auto;
    z-index: 2;
}

JS code: JS代码:

$(document).ready(function() {
    var movementStrength = 75;
    var height = movementStrength / $(window).height();
    var width = movementStrength / $(window).width();
    $(".firstPlane").mousemove(function(e) {
        var pageX = e.pageX - ($(window).width() / 2);
        var pageY = e.pageY - ($(window).height() / 2);
        var newvalueX = width * pageX * -1;
        var newvalueY = height * pageY * -1;
        $('#bkg-image').css("background-position", newvalueX + "px     " + newvalueY + "px");
    });
});

Any idea how to crop the excess background or how to rescale it to fit behind firstplane image?知道如何裁剪多余的背景或如何重新缩放以适合第一平面图像吗?

note: blur class is just for an animated blur effect, not relevant to this.注意:模糊 class 仅用于动画模糊效果,与此无关。 I took the java script from a net tutorial.我从网络教程中获取了 java 脚本。

My first aproach was using webkit mask-image, but seens it don't works, now I'm trying this method.我的第一个方法是使用 webkit mask-image,但发现它不起作用,现在我正在尝试这种方法。

Thanks a lot for any help非常感谢您的帮助

You are doing it upside down.你做的是颠倒的。 This is how it should be.这是应该的。

$(document).ready(function() {
    $("#bkg-image").mousemove(function(e) { 
        $('.firstPlane').css({left: e.pageX + "px", top: e.pageY + "px"});
    });
});

I need invert the selection: I need read the mouse position on first plane and apply movement to the bkg image.我需要反转选择:我需要在第一个平面上读取鼠标 position 并将移动应用于 bkg 图像。 (your script only works if I switch #bkg-image and.firstPlane). (您的脚本仅在我切换#bkg-image 和.firstPlane 时才有效)。 My issue isn't read the movement, but crop bkg-image to not show it bellow 1st plane when I have a small and tall browser window.我的问题不是读取运动,而是当我有一个又小又高的浏览器 window 时,裁剪 bkg-image 以使其不显示在第一平面下方。

The solution would be a mask/clip image, but its not working for svg graphics or PNG for me, so I give up and tried with a big black PNG with transp on it to show background.解决方案是蒙版/剪辑图像,但它不适用于 svg 图形或 PNG,所以我放弃并尝试使用带有 transp 的黑色大 PNG 来显示背景。 Something like this: (https://codepen.io/caraujo/pen/rVOZKJ) but with a logo (vector or png), but mask/image clip is not working for me:/像这样的东西: (https://codepen.io/caraujo/pen/rVOZKJ)但带有徽标(矢量或png),但蒙版/图像剪辑不适用于我:/

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

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