首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为单个图像创建多个蒙版

为单个图像创建多个蒙版
EN

Stack Overflow用户
提问于 2012-02-20 07:18:44
回答 2查看 2.3K关注 0票数 1

我正在尝试创建以下场景,并且我愿意尽可能地使用jquery、css和html的任何组合(但是,我现在不想使用HTML5,但如果它是唯一的解决方案,我愿意考虑它)。

我想有一个大的图像只可见的地方面具躺在它。我尝试了多种技术,但都不起作用。如有任何建议,我们将不胜感激!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-20 10:54:10

一种简单的方法是将图像设置为“窗口”上的background-image,这些窗口绝对位于#maskContainer中。设置了topleft,并将background-position设置为匹配。

请参阅: http://jsfiddle.net/thirtydot/XjCCK/

(在WebKit浏览器中查看,这样background-position动画就可以工作了;它纯粹是为了演示)

HTML:

代码语言:javascript
复制
<div id="maskContainer">
    <div class="window static"></div>
    <div class="window moving"></div>
</div>

CSS:

代码语言:javascript
复制
#maskContainer {
    background: #000;
    width: 603px;
    height: 482px;
    position: relative;
    overflow: hidden;
}
.window {
    background: url(http://i.imgur.com/j9a2T.jpg) no-repeat;
    position: absolute;
}
.static {
    width: 80px;
    height: 80px;
    left: 20px;
    top: 30px;
    background-position: -20px -30px;
}

JavaScript:

代码语言:javascript
复制
var maskContainerWidth = $('#maskContainer').width(),
    maskContainerHeight = $('#maskContainer').height();

setInterval(function() {
    $('.moving').each(function() {
        var width = Math.floor(Math.random()*maskContainerWidth),
            height = Math.floor(Math.random()*maskContainerHeight),
            left = Math.floor(Math.random()*(maskContainerWidth-width)),
            top = Math.floor(Math.random()*(maskContainerHeight-height));

        $(this).animate({
            width: width,
            height: height,
            left: left,
            top: top,
            backgroundPositionX: -left,
            backgroundPositionY: -top
        }, 1000);
    });
}, 1000);
票数 3
EN

Stack Overflow用户

发布于 2012-02-20 08:32:22

为什么不把你的“掩蔽div”周围的一切都当做掩码(因为它就是它的真面目)。使用带有透明孔的图像(掩蔽div所在的位置)或带有透明div的不透明div网格。将此图像/网格放在背景图像上。这将使您在将窗口移动到背景图像时具有很大的灵活性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9354321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档