javascript - 是否可以为您网站的特定部分制作图片?

标签 javascript jquery html css

如果这是一个愚蠢的问题,我深表歉意,但是有没有办法从网站的某个部分“拍照”并将其保存在计算机上?

HTML

<div id="red" color="red" width="100px" height="100px"></div>
<div id="green" color="green" width="100px" height="100px"></div>
<div id="blue" color="blue" width="100px" height="100px"></div>

例如,我有一个网页,其中有 3 个相邻的 div,我想只拍一张绿色 div 的照片,并将其保存为 jpeg 或其他格式。

例如,对于 JQuery/Javascript,这完全可能吗?

最佳答案

作为grigno说,Phantom.js将是一个不错的选择,因为它有 screen capture能力。

这是一个示例,说明如何使用 getBoundingClientRect 拍摄特定元素的照片.

JavaScript

var page = require('webpage').create();

address = 'https://stackoverflow.com/q/18466303/2129835';
page.open(address, function (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
    } else {
        window.setTimeout(function () {
            page.clipRect = page.evaluate(function() {
                return document.querySelector('.question .post-text').getBoundingClientRect(); 
            });
            page.render('output.png');
            phantom.exit();
        }, 200);
    }
});

结果:输出.png

enter image description here

关于javascript - 是否可以为您网站的特定部分制作图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18466303/

相关文章:

javascript - 一页中多种不同类型的模态框

javascript - 当 Chrome 中的选项卡处于非事件状态时,如何使 setInterval 也起作用?

javascript - 验证 Javascript 中的文本区域

css - 将 swiffy-Stage 对齐到页面右侧

javascript - SimpleSchema 匹配除 null 之外的任何类型

javascript - 如果表很大,我对表中元素的一点 ajax 就会中断。怎么修?

jquery - 试图复制向下滚动按钮悬停效果

html - <div> 之间的水平线

html - Css3 动画在 IE11 中不起作用

javascript - 如何创建jQuery元素缓存机制?