简体   繁体   English

响应头宽度两个重叠的图像

[英]Responsive header width two overlapping images

I am creating a responsive header with two overlapping images. 我正在创建带有两个重叠图像的响应式标题。 A div can be larger or smaller, based on the current mouse position. 根据当前鼠标位置,div可以更大或更小。 The problem is to create this header responsive. 问题是创建响应此标头。 The center of both images should be in the center of the webpage. 两张图片的中心应位于网页的中心。 This is hard to achieve as the div's that contain the images are not in the center of the document. 由于包含图像的div不在文档中心,因此很难实现。 You can see my current progress here . 您可以在此处查看我当前的进度。 Whenever the browser is around 1200 pixels it looks fine (the image width is 1280 px). 只要浏览器的像素为1200像素左右,它看起来就很好(图像宽度为1280像素)。 However, when i resize the browser to a smaller size the effect is not how it is meant to be. 但是,当我将浏览器调整为较小的尺寸时,其效果并非如此。

HTML HTML

<div class="header">
<div class="headerleft"><img src="images/rick.png" width="1280" height="200" id="rick" alt="rick" align="middle"></div>
<div class="headerright"><img src="images/peter.png" width="1280" height="200" id="peter" alt="peter" align="right"></div>
</div>

CSS CSS

    .header{
        height: 200px;
        width: 100%;
        background-size: 50%;
        background-position: center;
        background-repeat: no-repeat;
    }

    .headerleft{
        background-color: red;
        width: 100%;
        height: 100%;
        float: left;
        margin: 0;
        padding: 0;
        overflow:hidden;
        background-image: url('images/rick.png');
        background-position: left;
    }

    .headerright{
        background-color: blue;
        width: 50%;
        height: 100%;
        float: right;
        margin: 0;
        padding: 0;
        overflow:hidden;
        /*background-image: url('images/peter.png');*/
        background-position: right;
    }

Javascript 使用Javascript

$(document).on('mousemove', function(e){
    page = $( document ).width();
    pagecrop = page/2;  
    $('.headerright').css({
       width:  e.pageX/2+pagecrop/2,
    });
    });

    $(document).on('mousemove', function(e){
    page = $( document ).width();
    pagecrop = page/2;
    $('.headerleft').css({
       width:  pagecrop-e.pageX/2+pagecrop/2,
    });
    });

Got it working with the CSS transform translate property, and change that value with jquery. 使它与CSS transform转换属性一起使用,并使用jquery更改该值。 Thank you docodemore! 谢谢docodemore!

$(document).ready(function() { 
    var $winwidth = $(window).width();
    var width2 = 1280-$winwidth
    var width3 = width2/2
        $('#peter').css('transform', 'translate(' + width3  + 'px)');
        $('#rick').css('transform', 'translate(-' + width3  + 'px)');
    $(window).bind("resize", function(){ 
        var $winwidth = $(window).width();
        var width2 = 1280-$winwidth
        var width3 = width2/2
            $('#peter').css('transform', 'translate(' + width3 + 'px)');
            $('#rick').css('transform', 'translate(-' + width3  + 'px)');
    });
});

Have you tried removing the fixed pixel size of the images in the HTML and replacing with CSS or changing the 1280px value to 100% as such: 您是否尝试过删除HTML中图像的固定像素大小并替换为CSS或将1280px值更改为100%,例如:

<div class="header">
<div class="headerleft"><img src="images/rick.png" width="100%" height="200" id="rick" alt="rick"  align="middle"></div>
<div class="headerright"><img src="images/peter.png" width="100%" height="200" id="peter" alt="peter" align="right"></div>
</div>

Not sure if that's the desired effect you're after but worth a shot perhaps? 不确定这是否是您想要的效果,但值得一试吗?

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

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