❶ 图片上传前用JS代码进行预览并编辑裁剪区域
http://love21cn.msn.com.cn的图片上传功能后可以实现区域截图,也可以实现放大缩小...估计是用了JS来实现的:
var div_move = 0;
var IE = document.all?true:false;
var tempX,tempY,oldX,oldY;
var have_move = 0;
function grasp()
{
div_move = 1;
if(IE)
{
document.getElementById("source_div").setCapture();
}
}
function free()
{
div_move = 0;
have_move = 0;
document.getElementById("source_div").releaseCapture();
}
function getMouseXY(e)
{
if (IE)
{ // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft
tempY = event.clientY + document.body.scrollTop
}
else
{
// grab the x-y pos.s if browser is NS
tempX = e.pageX
tempY = e.pageY
}
// catch possible negative values in NS4
if (tempX < 0){tempX = 0}
if (tempY < 0){tempY = 0}
}
function move_it(e)
{
getMouseXY(e);
if(div_move == 1)
{
if(have_move == 0)
{
//alert('a');
oldX = tempX;
oldY = tempY;
have_move = 1;
}
var left = parseInt(document.getElementById("source_div").style.left);
var top = parseInt(document.getElementById("source_div").style.top);
//alert(top);
//alert(left);
//alert(tempX);
//alert(oldX);
document.getElementById("source_div").style.left = left + tempX - oldX;
document.getElementById("source_div").style.top = top + tempY - oldY;
oldX = tempX;
oldY = tempY;
}
}
function change_size(method)
{
if(method == 1)
{
var per = 1.25;
}
else
{
var per = 0.8;
}
document.getElementById("show_img").width = document.getElementById("show_img").width*per;
//document.getElementById("show_img").height = document.getElementById("show_img").height*per;
}
function micro_move(method)
{
switch (method)
{
case "up":
var top = parseInt(document.getElementById("source_div").style.top);
document.getElementById("source_div").style.top = top - 5;
break;
case "down":
var top = parseInt(document.getElementById("source_div").style.top);
document.getElementById("source_div").style.top = top + 5;
break;
case "left":
var left = parseInt(document.getElementById("source_div").style.left);
document.getElementById("source_div").style.left = left - 5;
break;
case "right":
var left = parseInt(document.getElementById("source_div").style.left);
document.getElementById("source_div").style.left = left + 5;
break;
}
}
function turn(method)
{
var i=document.getElementById('show_img').style.filter.match(/\d/)[0]
//alert(i);
i=parseInt(i)+parseInt(method);
//alert(i);
if(i<0)
{
i += 4;
}
if(i>=4)
{
i -= 4;
}
//alert(i);
document.getElementById('show_img').style.filter='progid:DXImageTransform.Microsoft.BasicImage(Rotation='+i+')'
}
function mysub()
{
var Oform = document.myform;
Oform.go.value = 1;
Oform.width.value = document.getElementById("show_img").width;
Oform.left.value = document.getElementById("source_div").style.left;
Oform.top.value = document.getElementById("source_div").style.top;
if(IE)
{
Oform.turn.value = document.getElementById('show_img').style.filter.match(/\d/)[0];
}
Oform.submit();
}
苹果树下也有类似功能不过,功能要比你所说的强大的多...
❷ 如何利用JS或者CSS样式来自动调整图片大小
这个使用CSS可以控制,一般看你的图片是哪种比例的,如果不想图片变形的话,只设置宽或者高,如:
img{ width:50px;} 或者 img{height:50px;}
这样的结果是,图片本身的大小并没有改变,但这个img标签显示出来的图片是被压缩显示的。
利用JS来调整的原理是:获得图片的宽和高,然后去与父容器的宽高相比,取比例大的来父容器值对img进行样式设置,比如:图片宽/父容器宽 = 3, 图片高/父容器高 = 2,那么取父容器的宽,把这个值通过JS设置给img标签。
❸ 用js实现图片编辑的功能
canvas, 2d graphic, raster graphic, mouse / pointer event
❹ 如何用JS语句显示图片
document.write("<imgsrc='图片地址'height=''weight=''alt=''/>");
其实很简单,用document对象里面的write方法。
当然你也可以弄一个按钮,然后点击按钮更改图片的属性,代码如下:
<buttononClick="pic.style.display='block';">点此显示图片</button>//按钮点击,图片显示
<imgsrc="../images/nb.jpg"border=0style="border:1px#000000solid;display:none"alt="盖章"name="audpic">//隐藏图片
❺ js如何在文本框后面加图片
插入光标处的插件
* @authors Du xin li
* @update 2015-10-25
*
*********************************************/
$.fn.extend({
insertContent : function(myValue, t) {
var that = $(this);
var $t = $(this)[0];
if (document.selection) {
this.focus();
var sel = document.selection.createRange();
sel.text = myValue;
this.focus();
sel.moveStart('character', -l);
var wee = sel.text.length;
if (arguments.length == 2) {
var l = $t.value.length;
sel.moveEnd("character", wee + t);
t <= 0 ? sel.moveStart("character", wee - 2 * t - myValue.length) : sel.moveStart("character", wee - t - myValue.length);
sel.select();
}
} else if ($t.selectionStart || $t.selectionStart == '0') {
var startPos = $t.selectionStart;
var endPos = $t.selectionEnd;
var scrollTop = $t.scrollTop;
$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos,$t.value.length);
this.focus();
$t.selectionStart = startPos + myValue.length;
$t.selectionEnd = startPos + myValue.length;
$t.scrollTop = scrollTop;
if (arguments.length == 2) {
$t.setSelectionRange(startPos - t,$t.selectionEnd + t);
this.focus();
}
} else {
this.value += myValue;
this.focus();
}
}
})
使用方法:
/**
* 点击表情按钮插入表情方法
* @param {string} dom 任意子节点
* @param {Object} event event对象
*/
fc.emote = function(dom, event){
if (dom) {
this.setParam(dom);
} else {
return false;
}
var that = $(dom);
var e = window.event || event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;
}
//显示表情弹出层
$('.fresh-dialog-emote').removeClass('hide');
//当前文本框textarea
var _currentTextarea = this.param.form;
//点击表情插入文本框
$('.fresh-dialog-emote').off('click', '.fresh-jsSmilies li').on('click', '.fresh-jsSmilies li', function(){
var _val = $(this).data('action');
console.log(_val)
console.log(_currentTextarea.length)
_currentTextarea.focus();
_currentTextarea.insertContent(_val);
$('.fresh-dialog-emote').addClass('hide');
})
}
// 点击表情按钮,弹出表情弹出层
$('.fresh-list').off('click', '.fresh-comment-emote-btn').on('click', '.fresh-comment-emote-btn', function(event){
fresh.comment.emote(this, event
❻ 我想在js页面获取jsp页面的一个div并在js里调用方法给它添加图片,用什么方法
给div设置background呗
不然往子节点append一个img
❼ 如何用JS实现简单的图片替换
用正则匹配<img />标签里面的src的内容, 找到后进行替换.