中文API文檔地址:http://code.ciaoca.com/jquery/jcrop/
簡單使用裁剪:
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <title>裁切插件</title> <!--一定要引入CSS--> <link rel="stylesheet" href="./Jcrop/css/Jcrop.min.css"> <style> /*.box { width: 600px; height: 400px; background: pink; }*/ </style> </head> <body> <!-- <div class="box"></div> --> <div class="box"> <img src="aa.jpg" alt=""> </div> <script src="../jquery/jquery.js"></script> <!-- Windows 是不區分大小寫 --> <!-- linux 是區分的 --> <script src="./Jcrop/js/Jcrop.min.js"></script> <script> // 有一個需要 // 寬度比為 2 // 任意大小的圖片,默認選區水平鋪滿,垂直居中 $('img').Jcrop({ // setSelect: [10, 10, 100, 100]//第一種設置模式,值是死的 aspectRatio: 2, //寬高比例 boxWidth: 400, //控制盒子寬度 // bgColor: 'red', canResize: false }, function () { // 當裁切插件執行成功后回調此方法 console.log('插件執行了...'); // this 指的是當前插件的實例 // console.log(this); // 此實例上有一些方法可供使用 // 原始圖片寬高 // this.ui.stage.width // this.ui.stage.height var w = this.ui.stage.width, h = w / 2, x = 0, y = (this.ui.stage.height - h) / 2; console.log(this); //開啟一個新的選區 this.newSelection(); this.setSelect([x, y, w, h]);//第二種設置模式值是可以設置獲取的 });// 事件監聽到父元素$('.box').on('cropstart cropmove cropend', function(e, s, c) { // console.log(e.type); console.log(c);});</script></body></html>截取時帶有預覽代碼:
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <title>裁切插件</title> <link rel="stylesheet" href="./Jcrop/css/Jcrop.min.css"> <style>/*.box { width: 600px; height: 400px; background: pink; }*/.jcrop-thumb { right: 0; top: 0;}.thumb { width: 300px; height: 400px; position: absolute; top: 0; right: 0; background: pink;}</style></head><body><!-- <div class="box"></div> --> <div class="box"> <img src="bb.jpg" alt=""> </div> <div class="thumb"></div> <script src="../jquery/jquery.js"></script> <!-- Windows 是不區分大小寫 --> <!-- Linux 是區分的 --> <script src="./Jcrop/js/Jcrop.js"></script> <script> // 有一個需要 // 寬度比為 2 // 任意大小的圖片,默認選區水平鋪滿,垂直居中 $('img').Jcrop({ // setSelect: [10, 10, 100, 100] // 設置選區的寬高比 aspectRatio: 2, // 設置圖片的寬度 boxWidth: 400, // bgColor: 'red', // 是否可以調整大小 // canResize: false }, function () { // 當裁切插件執行成功后回調此方法 console.log('插件執行了...'); // this 指的是當前插件的實例 // console.log(this); // 此實例上有一些方法可供使用 // 原始圖片寬高 // this.ui.stage.width // this.ui.stage.height var w = this.ui.stage.width, h = w / 2, x = 0, y = (this.ui.stage.height - h) / 2; // 新選區 this.newSelection(); // 設置新選區坐標 this.setSelect([x, y, w, h]); this.initComponent('Thumbnailer', { width: 240, height: 120, thumb: '.thumb' }); });// 事件監聽到父元素$('.box').on('cropstart cropmove cropend', function(e, s, c) { // console.log(e.type); // 通過c參數可以獲取裁切后的盡寸 console.log(c);});</script></body></html>
新聞熱點
疑難解答