how2j.cn

步骤 1 : 完整的图片   
步骤 2 : 使用背景方式   
步骤 3 : 使用img方式   

步骤 1 :

完整的图片

这是一个旺旺的动态图片,上面有各种旺旺的图

但是有的时候,我们只需要这个大图的一小部分,那么怎么做呢?
"); window.frames["iframe_show3088"].document.write(decodeHtml($("textarea#stepcodeTextarea3088").val())); window.frames["iframe_show3088"].document.close(); $(window.frames["iframe_show3088"]).load(function(){ $("#iframe_show3088").height($("#iframe_show3088").contents().find("body").height()+showittryitheight); }); $("#iframe_show3088").height($("#iframe_show3088").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<img src="http://127.0.0.1/study/wangwang.gif">
<img src="http://127.0.0.1/study/wangwang.gif">
"); window.frames["iframe3088"].document.write(decodeHtml(code3088)); window.frames["iframe3088"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe3088"]).load(function(){ $("#iframe3088").height($("#iframe3088").contents().find("body").height()+showittryitheight); }); $("#iframe3088").height($("#iframe3088").contents().find("body").height()+showittryitheight); alreadyWriteCode3088 = code3088; $("#rendering3088").hide(); $("#rendered3088").show(); } var tRereshRetry2DemoPanel3088 = setInterval(rereshRetry2DemoPanel3088,1000); var binded3088 = false; $("textarea#stepcodeTextarea3088").keyup(function(){ if(!binded3088){ $(window).bind('beforeunload',function(){ binded3088 = true; return "xxxx"; }); } var newCode = $(this).val() code3088 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code3088!=newCode){ // window.frames["iframe3088"].document.write("
"); // window.frames["iframe3088"].document.write(decodeHtml($("textarea#stepcodeTextarea3088").val())); // window.frames["iframe3088"].document.close(); // $(window.frames["iframe3088"]).load(function(){ // $("#iframe3088").height($("#iframe3088").contents().find("body").height()+showittryitheight); // }); // code3088 = newCode; // } }); $(".tryButton3088").click(function(){ $("#tryDiv3088").show(); $("#stepcodeTextarea3088").focus(); $("#stepcodeTextarea3088").height(200); $("#iframe3088").height(0); window.frames["iframe3088"].document.write("
"); window.frames["iframe3088"].document.write(decodeHtml($("textarea#stepcodeTextarea3088").val())); window.frames["iframe3088"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe3088"]).load(function(){ $("#iframe3088").height($("#iframe3088").contents().find("body").height()+showittryitheight); }); $("#iframe3088").height($("#iframe3088").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor3088.focus(); editor3088.setSize(null, "250"); $("#rendering3088").hide(); $("#rendered3088").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor3088 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea3088"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor3088.on("change",function(doc){ if(!binded3088){ $(window).bind('beforeunload',function(){ binded3088 = true; return "xxxx"; }); } var newCode = doc.getValue(); code3088 = newCode; $("textarea#stepcodeTextarea3088").val(newCode); if(alreadyWriteCode3088!=code3088){ lastModifedTime3088 = new Date().getTime(); $("#rendering3088").show(); $("#rendered3088").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor3088 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor3088.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv3088").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
步骤 2 :

使用背景方式

第一种方式,使用背景的方式获取一部分图片

对div使用背景图片

background:transparent url(/study/wangwang.gif) no-repeat scroll -83px -0px ;

等同于

background-color:transparent;
background-image:url(/site/wangwang.gif);
background-repeat:no-repeat;
background-attachment:scroll;
background-position: -83px -0px;


设置scroll -83px -0px 把图片向左滚动83个像素,向上滚动0个像素。
再把div大小设置为和小图片大小一样,即可大小只显示部分图片的效果
"); window.frames["iframe_show3089"].document.write(decodeHtml($("textarea#stepcodeTextarea3089").val())); window.frames["iframe_show3089"].document.close(); $(window.frames["iframe_show3089"]).load(function(){ $("#iframe_show3089").height($("#iframe_show3089").contents().find("body").height()+showittryitheight); }); $("#iframe_show3089").height($("#iframe_show3089").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<style> div{ width:25; height:25; background:transparent url(http://127.0.0.1/study/wangwang.gif) no-repeat scroll -83px -0px ; } </style> <div></div>
<style>
div{
	width:25;
	height:25;
	background:transparent url(http://127.0.0.1/study/wangwang.gif) no-repeat scroll -83px -0px ;
}
 
</style>

<div></div>
"); window.frames["iframe3089"].document.write(decodeHtml(code3089)); window.frames["iframe3089"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe3089"]).load(function(){ $("#iframe3089").height($("#iframe3089").contents().find("body").height()+showittryitheight); }); $("#iframe3089").height($("#iframe3089").contents().find("body").height()+showittryitheight); alreadyWriteCode3089 = code3089; $("#rendering3089").hide(); $("#rendered3089").show(); } var tRereshRetry2DemoPanel3089 = setInterval(rereshRetry2DemoPanel3089,1000); var binded3089 = false; $("textarea#stepcodeTextarea3089").keyup(function(){ if(!binded3089){ $(window).bind('beforeunload',function(){ binded3089 = true; return "xxxx"; }); } var newCode = $(this).val() code3089 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code3089!=newCode){ // window.frames["iframe3089"].document.write("
"); // window.frames["iframe3089"].document.write(decodeHtml($("textarea#stepcodeTextarea3089").val())); // window.frames["iframe3089"].document.close(); // $(window.frames["iframe3089"]).load(function(){ // $("#iframe3089").height($("#iframe3089").contents().find("body").height()+showittryitheight); // }); // code3089 = newCode; // } }); $(".tryButton3089").click(function(){ $("#tryDiv3089").show(); $("#stepcodeTextarea3089").focus(); $("#stepcodeTextarea3089").height(200); $("#iframe3089").height(0); window.frames["iframe3089"].document.write("
"); window.frames["iframe3089"].document.write(decodeHtml($("textarea#stepcodeTextarea3089").val())); window.frames["iframe3089"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe3089"]).load(function(){ $("#iframe3089").height($("#iframe3089").contents().find("body").height()+showittryitheight); }); $("#iframe3089").height($("#iframe3089").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor3089.focus(); editor3089.setSize(null, "250"); $("#rendering3089").hide(); $("#rendered3089").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor3089 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea3089"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor3089.on("change",function(doc){ if(!binded3089){ $(window).bind('beforeunload',function(){ binded3089 = true; return "xxxx"; }); } var newCode = doc.getValue(); code3089 = newCode; $("textarea#stepcodeTextarea3089").val(newCode); if(alreadyWriteCode3089!=code3089){ lastModifedTime3089 = new Date().getTime(); $("#rendering3089").show(); $("#rendered3089").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor3089 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor3089.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv3089").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
步骤 3 :

使用img方式

借助裁剪的方式只显示部分图片:

clip:rect(top, right, bottom, left)


裁剪之后,只显示被裁剪出来的图片,所以还需要把整个图片向左移动,才看上去像拿到了想要的那部分图片

img{
position:absolute;
left:-83px;
clip:rect(0px 108px 25px 83px);
}
"); window.frames["iframe_show3090"].document.write(decodeHtml($("textarea#stepcodeTextarea3090").val())); window.frames["iframe_show3090"].document.close(); $(window.frames["iframe_show3090"]).load(function(){ $("#iframe_show3090").height($("#iframe_show3090").contents().find("body").height()+showittryitheight); }); $("#iframe_show3090").height($("#iframe_show3090").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<style> img{ position:absolute; left:-83px; clip:rect(0px 108px 25px 83px); } </style> <img src="http://127.0.0.1/study/wangwang.gif">
<style>
img{
    position:absolute;
    left:-83px;
    clip:rect(0px 108px 25px 83px);
} 
</style>

<img src="http://127.0.0.1/study/wangwang.gif">
"); window.frames["iframe3090"].document.write(decodeHtml(code3090)); window.frames["iframe3090"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe3090"]).load(function(){ $("#iframe3090").height($("#iframe3090").contents().find("body").height()+showittryitheight); }); $("#iframe3090").height($("#iframe3090").contents().find("body").height()+showittryitheight); alreadyWriteCode3090 = code3090; $("#rendering3090").hide(); $("#rendered3090").show(); } var tRereshRetry2DemoPanel3090 = setInterval(rereshRetry2DemoPanel3090,1000); var binded3090 = false; $("textarea#stepcodeTextarea3090").keyup(function(){ if(!binded3090){ $(window).bind('beforeunload',function(){ binded3090 = true; return "xxxx"; }); } var newCode = $(this).val() code3090 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code3090!=newCode){ // window.frames["iframe3090"].document.write("
"); // window.frames["iframe3090"].document.write(decodeHtml($("textarea#stepcodeTextarea3090").val())); // window.frames["iframe3090"].document.close(); // $(window.frames["iframe3090"]).load(function(){ // $("#iframe3090").height($("#iframe3090").contents().find("body").height()+showittryitheight); // }); // code3090 = newCode; // } }); $(".tryButton3090").click(function(){ $("#tryDiv3090").show(); $("#stepcodeTextarea3090").focus(); $("#stepcodeTextarea3090").height(200); $("#iframe3090").height(0); window.frames["iframe3090"].document.write("
"); window.frames["iframe3090"].document.write(decodeHtml($("textarea#stepcodeTextarea3090").val())); window.frames["iframe3090"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe3090"]).load(function(){ $("#iframe3090").height($("#iframe3090").contents().find("body").height()+showittryitheight); }); $("#iframe3090").height($("#iframe3090").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor3090.focus(); editor3090.setSize(null, "250"); $("#rendering3090").hide(); $("#rendered3090").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor3090 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea3090"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor3090.on("change",function(doc){ if(!binded3090){ $(window).bind('beforeunload',function(){ binded3090 = true; return "xxxx"; }); } var newCode = doc.getValue(); code3090 = newCode; $("textarea#stepcodeTextarea3090").val(newCode); if(alreadyWriteCode3090!=code3090){ lastModifedTime3090 = new Date().getTime(); $("#rendering3090").show(); $("#rendered3090").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor3090 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor3090.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv3090").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-CSS-显示图片一部分 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图