首页 web前端 js教程 jQuery实现图片局部放大镜效果_jquery

jQuery实现图片局部放大镜效果_jquery

May 16, 2016 pm 03:10 PM

下图只是给大家举个例子,类似于这种效果图:

具体实现过程请看下文代码:

css

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

.zoomPad{

position:relative;

float:left;

z-index:99;

cursor:crosshair;

}

.zoomPreload{

-moz-opacity:0.8;

opacity: 0.8;

filter: alpha(opacity = 80);

color: #333;

font-size: 12px;

font-family: Tahoma;

text-decoration: none;

border: 1px solid #CCC;

padding: 8px;

text-align:center;

background-image: url(../images/zoomloader.gif);

background-repeat: no-repeat;

background-position: 43px 30px;

z-index:110;

width:90px;

height:43px;

position:absolute;

top:0px;

left:0px;

* width:100px;

* height:49px;

}

.zoomPup{

overflow:hidden;

-moz-opacity:0.6;

opacity: 0.6;

filter: alpha(opacity = 60);

z-index:120;

position:absolute;

border:1px solid #CCC;

z-index:101;

cursor:crosshair;

}

.zoomOverlay{

position:absolute;

left:0px;

top:0px;

/*opacity:0.5;*/

z-index:5000;

width:100%;

height:100%;

display:none;

z-index:101;

}

.zoomWindow{

position:absolute;

left:110%;

top:40px;

z-index:6000;

height:auto;

z-index:10000;

z-index:110;

}

.zoomWrapper{

position:relative;

border:1px solid #999;

z-index:110;

}

.zoomWrapperTitle{

display:block;

background:#999;

color:#FFF;

height:18px;

line-height:18px;

width:100%;

overflow:hidden;

text-align:center;

font-size:10px;

position:absolute;

top:0px;

left:0px;

z-index:120;

-moz-opacity:0.6;

opacity: 0.6;

filter: alpha(opacity = 60);

}

.zoomWrapperImage{

display:block;

position:relative;

overflow:hidden;

z-index:110;

}

.zoomWrapperImage img{

border:0px;

display:block;

position:absolute;

z-index:101;

}

.zoomIframe{

z-index: -1;

filter:alpha(opacity=0);

-moz-opacity: 0.80;

opacity: 0.80;

position:absolute;

display:block;

}

/*********************************************************

/ When clicking on thumbs jqzoom will add the class

/ "zoomThumbActive" on the anchor selected

/*********************************************************/

登录后复制

js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

/*!

* jQzoom Evolution Library v2.3 - Javascript Image magnifier

* http://www.mind-projects.it

*

* Copyright 2011, Engineer Marco Renzi

* Licensed under the BSD license.

*

* Redistribution and use in source and binary forms, with or without

* modification, are permitted provided that the following conditions are met:

* * Redistributions of source code must retain the above copyright

* notice, this list of conditions and the following disclaimer.

* * Redistributions in binary form must reproduce the above copyright

* notice, this list of conditions and the following disclaimer in the

* documentation and/or other materials provided with the distribution.

* * Neither the name of the organization nor the

* names of its contributors may be used to endorse or promote products

* derived from this software without specific prior written permission.

*

* Date: 03 May 2011 22:16:00

*/

(function ($) {

//GLOBAL VARIABLES

var isIE6 = ($.browser.msie && $.browser.version < 7);

var body = $(document.body);

var window = $(window);

var jqzoompluging_disabled = false; //disabilita globalmente il plugin

$.fn.jqzoom = function (options) {

return this.each(function () {

var node = this.nodeName.toLowerCase();

if (node == 'a') {

new jqzoom(this, options);

}

});

};

jqzoom = function (el, options) {

var api = null;

api = $(el).data("jqzoom");

if (api) return api;

var obj = this;

var settings = $.extend({}, $.jqzoom.defaults, options || {});

obj.el = el;

el.rel = $(el).attr('rel');

//ANCHOR ELEMENT

el.zoom_active = false;

el.zoom_disabled = false; //to disable single zoom instance

el.largeimageloading = false; //tell us if large image is loading

el.largeimageloaded = false; //tell us if large image is loaded

el.scale = {};

el.timer = null;

el.mousepos = {};

el.mouseDown = false;

$(el).css({

'outline-style': 'none',

'text-decoration': 'none'

});

//BASE IMAGE

var img = $("img:eq(0)", el);

el.title = $(el).attr('title');

el.imagetitle = img.attr('title');

var zoomtitle = ($.trim(el.title).length > 0) &#63; el.title : el.imagetitle;

var smallimage = new Smallimage(img);

var lens = new Lens();

var stage = new Stage();

var largeimage = new Largeimage();

var loader = new Loader();

//preventing default click,allowing the onclick event [exmple: lightbox]

$(el).bind('click', function (e) {

e.preventDefault();

return false;

});

//setting the default zoomType if not in settings

var zoomtypes = ['standard', 'drag', 'innerzoom', 'reverse'];

if ($.inArray($.trim(settings.zoomType), zoomtypes) < 0) {

settings.zoomType = 'standard';

}

$.extend(obj, {

create: function () { //create the main objects

//create ZoomPad

if ($(".zoomPad", el).length == 0) {

el.zoomPad = $('<div/>').addClass('zoomPad');

img.wrap(el.zoomPad);

}

if(settings.zoomType == 'innerzoom'){

settings.zoomWidth = smallimage.w;

settings.zoomHeight = smallimage.h;

}

//creating ZoomPup

if ($(".zoomPup", el).length == 0) {

lens.append();

}

//creating zoomWindow

if ($(".zoomWindow", el).length == 0) {

stage.append();

}

//creating Preload

if ($(".zoomPreload", el).length == 0) {

loader.append();

}

//preloading images

if (settings.preloadImages || settings.zoomType == 'drag' || settings.alwaysOn) {

obj.load();

}

obj.init();

},

init: function () {

//drag option

if (settings.zoomType == 'drag') {

$(".zoomPad", el).mousedown(function () {

el.mouseDown = true;

});

$(".zoomPad", el).mouseup(function () {

el.mouseDown = false;

});

document.body.ondragstart = function () {

return false;

};

$(".zoomPad", el).css({

cursor: 'default'

});

$(".zoomPup", el).css({

cursor: 'move'

});

}

if (settings.zoomType == 'innerzoom') {

$(".zoomWrapper", el).css({

cursor: 'crosshair'

});

}

$(".zoomPad", el).bind('mouseenter mouseover', function (event) {

img.attr('title', '');

$(el).attr('title', '');

el.zoom_active = true;

//if loaded then activate else load large image

smallimage.fetchdata();

if (el.largeimageloaded) {

obj.activate(event);

} else {

obj.load();

}

});

$(".zoomPad", el).bind('mouseleave', function (event) {

obj.deactivate();

});

$(".zoomPad", el).bind('mousemove', function (e) {

//prevent fast mouse mevements not to fire the mouseout event

if (e.pageX > smallimage.pos.r || e.pageX < smallimage.pos.l || e.pageY < smallimage.pos.t || e.pageY > smallimage.pos.b) {

lens.setcenter();

return false;

}

el.zoom_active = true;

if (el.largeimageloaded && !$('.zoomWindow', el).is(':visible')) {

obj.activate(e);

}

if (el.largeimageloaded && (settings.zoomType != 'drag' || (settings.zoomType == 'drag' && el.mouseDown))) {

lens.setposition(e);

}

});

var thumb_preload = new Array();

var i = 0;

//binding click event on thumbnails

var thumblist = new Array();

thumblist = $('a').filter(function () {

var regex = new RegExp("gallery[\\s]*:[\\s]*'" + $.trim(el.rel) + "'", "i");

var rel = $(this).attr('rel');

if (regex.test(rel)) {

return this;

}

});

if (thumblist.length > 0) {

//getting the first to the last

var first = thumblist.splice(0, 1);

thumblist.push(first);

}

thumblist.each(function () {

//preloading thumbs

if (settings.preloadImages) {

var thumb_options = $.extend({}, eval("(" + $.trim($(this).attr('rel')) + ")"));

thumb_preload[i] = new Image();

thumb_preload[i].src = thumb_options.largeimage;

i++;

}

$(this).click(function (e) {

if($(this).hasClass('zoomThumbActive')){

return false;

}

thumblist.each(function () {

$(this).removeClass('zoomThumbActive');

});

e.preventDefault();

obj.swapimage(this);

return false;

});

});

},

load: function () {

if (el.largeimageloaded == false && el.largeimageloading == false) {

var url = $(el).attr('href');

el.largeimageloading = true;

largeimage.loadimage(url);

}

},

activate: function (e) {

clearTimeout(el.timer);

//show lens and zoomWindow

lens.show();

stage.show();

},

deactivate: function (e) {

switch (settings.zoomType) {

case 'drag':

//nothing or lens.setcenter();

break;

default:

img.attr('title', el.imagetitle);

$(el).attr('title', el.title);

if (settings.alwaysOn) {

lens.setcenter();

} else {

stage.hide();

lens.hide();

}

break;

}

el.zoom_active = false;

},

swapimage: function (link) {

el.largeimageloading = false;

el.largeimageloaded = false;

var options = new Object();

options = $.extend({}, eval("(" + $.trim($(link).attr('rel')) + ")"));

if (options.smallimage && options.largeimage) {

var smallimage = options.smallimage;

var largeimage = options.largeimage;

$(link).addClass('zoomThumbActive');

$(el).attr('href', largeimage);

img.attr('src', smallimage);

lens.hide();

stage.hide();

obj.load();

} else {

alert('ERROR :: Missing parameter for largeimage or smallimage.');

throw 'ERROR :: Missing parameter for largeimage or smallimage.';

}

return false;

}

});

//sometimes image is already loaded and onload will not fire

if (img[0].complete) {

//fetching data from sallimage if was previously loaded

smallimage.fetchdata();

if ($(".zoomPad", el).length == 0) obj.create();

}

/*========================================================,

| Smallimage

|---------------------------------------------------------:

| Base image into the anchor element

`========================================================*/

function Smallimage(image) {

var $obj = this;

this.node = image[0];

this.findborder = function () {

var bordertop = 0;

bordertop = image.css('border-top-width');

btop = '';

var borderleft = 0;

borderleft = image.css('border-left-width');

bleft = '';

if (bordertop) {

for (i = 0; i < 3; i++) {

var x = [];

x = bordertop.substr(i, 1);

if (isNaN(x) == false) {

btop = btop + '' + bordertop.substr(i, 1);

} else {

break;

}

}

}

if (borderleft) {

for (i = 0; i < 3; i++) {

if (!isNaN(borderleft.substr(i, 1))) {

bleft = bleft + borderleft.substr(i, 1)

} else {

break;

}

}

}

$obj.btop = (btop.length > 0) &#63; eval(btop) : 0;

$obj.bleft = (bleft.length > 0) &#63; eval(bleft) : 0;

};

this.fetchdata = function () {

$obj.findborder();

$obj.w = image.width();

$obj.h = image.height();

$obj.ow = image.outerWidth();

$obj.oh = image.outerHeight();

$obj.pos = image.offset();

$obj.pos.l = image.offset().left + $obj.bleft;

$obj.pos.t = image.offset().top + $obj.btop;

$obj.pos.r = $obj.w + $obj.pos.l;

$obj.pos.b = $obj.h + $obj.pos.t;

$obj.rightlimit = image.offset().left + $obj.ow;

$obj.bottomlimit = image.offset().top + $obj.oh;

};

this.node.onerror = function () {

alert('Problems while loading image.');

throw 'Problems while loading image.';

};

this.node.onload = function () {

$obj.fetchdata();

if ($(".zoomPad", el).length == 0) obj.create();

};

return $obj;

};

/*========================================================,

| Loader

|---------------------------------------------------------:

| Show that the large image is loading

`========================================================*/

function Loader() {

var $obj = this;

this.append = function () {

this.node = $('<div/>').addClass('zoomPreload').css('visibility', 'hidden').html(settings.preloadText);

$('.zoomPad', el).append(this.node);

};

this.show = function () {

this.node.top = (smallimage.oh - this.node.height()) / 2;

this.node.left = (smallimage.ow - this.node.width()) / 2;

//setting position

this.node.css({

top: this.node.top,

left: this.node.left,

position: 'absolute',

visibility: 'visible'

});

};

this.hide = function () {

this.node.css('visibility', 'hidden');

};

return this;

}

/*========================================================,

| Lens

|---------------------------------------------------------:

| Lens over the image

`========================================================*/

function Lens() {

var $obj = this;

this.node = $('<div/>').addClass('zoomPup');

//this.nodeimgwrapper = $("<div/>").addClass('zoomPupImgWrapper');

this.append = function () {

$('.zoomPad', el).append($(this.node).hide());

if (settings.zoomType == 'reverse') {

this.image = new Image();

this.image.src = smallimage.node.src; // fires off async

$(this.node).empty().append(this.image);

}

};

this.setdimensions = function () {

this.node.w = (parseInt((settings.zoomWidth) / el.scale.x) > smallimage.w ) &#63; smallimage.w : (parseInt(settings.zoomWidth / el.scale.x));

this.node.h = (parseInt((settings.zoomHeight) / el.scale.y) > smallimage.h ) &#63; smallimage.h : (parseInt(settings.zoomHeight / el.scale.y));

this.node.top = (smallimage.oh - this.node.h - 2) / 2;

this.node.left = (smallimage.ow - this.node.w - 2) / 2;

//centering lens

this.node.css({

top: 0,

left: 0,

width: this.node.w + 'px',

height: this.node.h + 'px',

position: 'absolute',

display: 'none',

borderWidth: 1 + 'px'

});

if (settings.zoomType == 'reverse') {

this.image.src = smallimage.node.src;

$(this.node).css({

'opacity': 1

});

$(this.image).css({

position: 'absolute',

display: 'block',

left: -(this.node.left + 1 - smallimage.bleft) + 'px',

top: -(this.node.top + 1 - smallimage.btop) + 'px'

});

}

};

this.setcenter = function () {

//calculating center position

this.node.top = (smallimage.oh - this.node.h - 2) / 2;

this.node.left = (smallimage.ow - this.node.w - 2) / 2;

//centering lens

this.node.css({

top: this.node.top,

left: this.node.left

});

if (settings.zoomType == 'reverse') {

$(this.image).css({

position: 'absolute',

display: 'block',

left: -(this.node.left + 1 - smallimage.bleft) + 'px',

top: -(this.node.top + 1 - smallimage.btop) + 'px'

});

}

//centering large image

largeimage.setposition();

};

this.setposition = function (e) {

el.mousepos.x = e.pageX;

el.mousepos.y = e.pageY;

var lensleft = 0;

var lenstop = 0;

function overleft(lens) {

return el.mousepos.x - (lens.w) / 2 < smallimage.pos.l;

}

function overright(lens) {

return el.mousepos.x + (lens.w) / 2 > smallimage.pos.r;

}

function overtop(lens) {

return el.mousepos.y - (lens.h) / 2 < smallimage.pos.t;

}

function overbottom(lens) {

return el.mousepos.y + (lens.h) / 2 > smallimage.pos.b;

}

lensleft = el.mousepos.x + smallimage.bleft - smallimage.pos.l - (this.node.w + 2) / 2;

lenstop = el.mousepos.y + smallimage.btop - smallimage.pos.t - (this.node.h + 2) / 2;

if (overleft(this.node)) {

lensleft = smallimage.bleft - 1;

} else if (overright(this.node)) {

lensleft = smallimage.w + smallimage.bleft - this.node.w - 1;

}

if (overtop(this.node)) {

lenstop = smallimage.btop - 1;

} else if (overbottom(this.node)) {

lenstop = smallimage.h + smallimage.btop - this.node.h - 1;

}

this.node.left = lensleft;

this.node.top = lenstop;

this.node.css({

'left': lensleft + 'px',

'top': lenstop + 'px'

});

if (settings.zoomType == 'reverse') {

if ($.browser.msie && $.browser.version > 7) {

$(this.node).empty().append(this.image);

}

$(this.image).css({

position: 'absolute',

display: 'block',

left: -(this.node.left + 1 - smallimage.bleft) + 'px',

top: -(this.node.top + 1 - smallimage.btop) + 'px'

});

}

largeimage.setposition();

};

this.hide = function () {

img.css({

'opacity': 1

});

this.node.hide();

};

this.show = function () {

if (settings.zoomType != 'innerzoom' && (settings.lens || settings.zoomType == 'drag')) {

this.node.show();

}

if (settings.zoomType == 'reverse') {

img.css({

'opacity': settings.imageOpacity

});

}

};

this.getoffset = function () {

var o = {};

o.left = $obj.node.left;

o.top = $obj.node.top;

return o;

};

return this;

};

/*========================================================,

| Stage

|---------------------------------------------------------:

| Window area that contains the large image

`========================================================*/

function Stage() {

var $obj = this;

this.node = $("<div class='zoomWindow'><div class='zoomWrapper'><div class='zoomWrapperTitle'></div><div class='zoomWrapperImage'></div></div></div>");

this.ieframe = $('<iframe class="zoomIframe" src="javascript:\'\';" marginwidth="0" marginheight="0" align="bottom" scrolling="no" frameborder="0" ></iframe>');

this.setposition = function () {

this.node.leftpos = 0;

this.node.toppos = 0;

if (settings.zoomType != 'innerzoom') {

//positioning

switch (settings.position) {

case "left":

this.node.leftpos = (smallimage.pos.l - smallimage.bleft - Math.abs(settings.xOffset) - settings.zoomWidth > 0) &#63; (0 - settings.zoomWidth - Math.abs(settings.xOffset)) : (smallimage.ow + Math.abs(settings.xOffset));

this.node.toppos = Math.abs(settings.yOffset);

break;

case "top":

this.node.leftpos = Math.abs(settings.xOffset);

this.node.toppos = (smallimage.pos.t - smallimage.btop - Math.abs(settings.yOffset) - settings.zoomHeight > 0) &#63; (0 - settings.zoomHeight - Math.abs(settings.yOffset)) : (smallimage.oh + Math.abs(settings.yOffset));

break;

case "bottom":

this.node.leftpos = Math.abs(settings.xOffset);

this.node.toppos = (smallimage.pos.t - smallimage.btop + smallimage.oh + Math.abs(settings.yOffset) + settings.zoomHeight < screen.height) &#63; (smallimage.oh + Math.abs(settings.yOffset)) : (0 - settings.zoomHeight - Math.abs(settings.yOffset));

break;

default:

this.node.leftpos = (smallimage.rightlimit + Math.abs(settings.xOffset) + settings.zoomWidth < screen.width) &#63; (smallimage.ow + Math.abs(settings.xOffset)) : (0 - settings.zoomWidth - Math.abs(settings.xOffset));

this.node.toppos = Math.abs(settings.yOffset);

break;

}

}

this.node.css({

'left': this.node.leftpos + 'px',

'top': this.node.toppos + 'px'

});

return this;

};

this.append = function () {

$('.zoomPad', el).append(this.node);

this.node.css({

position: 'absolute',

display: 'none',

zIndex: 5001

});

if (settings.zoomType == 'innerzoom') {

this.node.css({

cursor: 'default'

});

var thickness = (smallimage.bleft == 0) &#63; 1 : smallimage.bleft;

$('.zoomWrapper', this.node).css({

borderWidth: thickness + 'px'

});

}

$('.zoomWrapper', this.node).css({

width: Math.round(settings.zoomWidth) + 'px' ,

borderWidth: thickness + 'px'

});

$('.zoomWrapperImage', this.node).css({

width: '100%',

height: Math.round(settings.zoomHeight) + 'px'

});

//zoom title

$('.zoomWrapperTitle', this.node).css({

width: '100%',

position: 'absolute'

});

$('.zoomWrapperTitle', this.node).hide();

if (settings.title && zoomtitle.length > 0) {

$('.zoomWrapperTitle', this.node).html(zoomtitle).show();

}

$obj.setposition();

};

this.hide = function () {

switch (settings.hideEffect) {

case 'fadeout':

this.node.fadeOut(settings.fadeoutSpeed, function () {});

break;

default:

this.node.hide();

break;

}

this.ieframe.hide();

};

this.show = function () {

switch (settings.showEffect) {

case 'fadein':

this.node.fadeIn();

this.node.fadeIn(settings.fadeinSpeed, function () {});

break;

default:

this.node.show();

break;

}

if (isIE6 && settings.zoomType != 'innerzoom') {

this.ieframe.width = this.node.width();

this.ieframe.height = this.node.height();

this.ieframe.left = this.node.leftpos;

this.ieframe.top = this.node.toppos;

this.ieframe.css({

display: 'block',

position: "absolute",

left: this.ieframe.left,

top: this.ieframe.top,

zIndex: 99,

width: this.ieframe.width + 'px',

height: this.ieframe.height + 'px'

});

$('.zoomPad', el).append(this.ieframe);

this.ieframe.show();

};

};

};

/*========================================================,

| LargeImage

|---------------------------------------------------------:

| The large detailed image

`========================================================*/

function Largeimage() {

var $obj = this;

this.node = new Image();

this.loadimage = function (url) {

//showing preload

loader.show();

this.url = url;

this.node.style.position = 'absolute';

this.node.style.border = '0px';

this.node.style.display = 'none';

this.node.style.left = '-5000px';

this.node.style.top = '0px';

document.body.appendChild(this.node);

this.node.src = url; // fires off async

};

this.fetchdata = function () {

var image = $(this.node);

var scale = {};

this.node.style.display = 'block';

$obj.w = image.width();

$obj.h = image.height();

$obj.pos = image.offset();

$obj.pos.l = image.offset().left;

$obj.pos.t = image.offset().top;

$obj.pos.r = $obj.w + $obj.pos.l;

$obj.pos.b = $obj.h + $obj.pos.t;

scale.x = ($obj.w / smallimage.w);

scale.y = ($obj.h / smallimage.h);

el.scale = scale;

document.body.removeChild(this.node);

$('.zoomWrapperImage', el).empty().append(this.node);

//setting lens dimensions;

lens.setdimensions();

};

this.node.onerror = function () {

alert('Problems while loading the big image.');

throw 'Problems while loading the big image.';

};

this.node.onload = function () {

//fetching data

$obj.fetchdata();

loader.hide();

el.largeimageloading = false;

el.largeimageloaded = true;

if (settings.zoomType == 'drag' || settings.alwaysOn) {

lens.show();

stage.show();

lens.setcenter();

}

};

this.setposition = function () {

var left = -el.scale.x * (lens.getoffset().left - smallimage.bleft + 1);

var top = -el.scale.y * (lens.getoffset().top - smallimage.btop + 1);

$(this.node).css({

'left': left + 'px',

'top': top + 'px'

});

};

return this;

};

$(el).data("jqzoom", obj);

};

//es. $.jqzoom.disable('#jqzoom1');

$.jqzoom = {

defaults: {

zoomType: 'standard',

//innerzoom/standard/reverse/drag

zoomWidth: 300,

//zoomWindow default width

zoomHeight: 300,

//zoomWindow default height

xOffset: 10,

//zoomWindow x offset, can be negative(more on the left) or positive(more on the right)

yOffset: 0,

//zoomWindow y offset, can be negative(more on the left) or positive(more on the right)

position: "right",

//zoomWindow default position

preloadImages: true,

//image preload

preloadText: 'Loading zoom',

title: true,

lens: true,

imageOpacity: 0.4,

alwaysOn: false,

showEffect: 'show',

//show/fadein

hideEffect: 'hide',

//hide/fadeout

fadeinSpeed: 'slow',

//fast/slow/number

fadeoutSpeed: '2000' //fast/slow/number

},

disable: function (el) {

var api = $(el).data('jqzoom');

api.disable();

return false;

},

enable: function (el) {

var api = $(el).data('jqzoom');

api.enable();

return false;

},

disableAll: function (el) {

jqzoompluging_disabled = true;

},

enableAll: function (el) {

jqzoompluging_disabled = false;

}

};

})(jQuery);

登录后复制

还需要有Jquery.js

使用方法:

1.当页面导入的时候,载入 jQZoom 插件。

1

2

3

4

5

6

7

8

9

10

11

12

$(function(){

$(".jqzoom").jqzoom({

zoomWidth: 300,

zoomHeight: 300,

lens:true,

preloadImages: false,

alwaysOn:false,

title:false,

xOffset:20,

position: "right"

});

})

登录后复制

2.创建一个放图片的容器,指定一个a标记用于显示放大后的图片的一部分:

1

2

3

<a href="images/BIGIMAGE.JPG" class="jqzoom" title="MYTITLE">

<img src="images/SMALLIMAGE.JPG" title="IMAGE TITLE">

</a>

登录后复制

下面给一些基本的配置参数:

•zoomType,默认值:'standard',另一个值是'reverse',是否将原图用半透明图层遮盖。

•zoomWidth,默认值:200,放大窗口的宽度。

•zoomHeight,默认值:200,放大窗口的高度。

•xOffset,默认值:10,放大窗口相对于原图的x轴偏移值,可以为负。

•yOffset,默认值:0,放大窗口相对于原图的y轴偏移值,可以为负。

•position,默认值:'right',放大窗口的位置,值还可以是:'right' ,'left' ,'top' ,'bottom'。

•lens,默认值:true,若为false,则不在原图上显示镜头。

•imageOpacity,默认值:0.2,当zoomType的值为'reverse'时,这个参数用于指定遮罩的透明度。

•title,默认值:true,在放大窗口中显示标题,值可以为a标记的title值,若无,则为原图的title值。

•showEffect,默认值:'show',显示放大窗口时的效果,值可以为: ‘show' ,'fadein'。

•hideEffect,默认值:'hide',隐藏放大窗口时的效果: ‘hide' ,'fadeout'。

•fadeinSpeed,默认值:'fast',放大窗口的渐显速度(选项: ‘fast','slow','medium')。

•fadeoutSpeed,默认值:'slow',放大窗口的渐隐速度(选项: ‘fast','slow','medium')。

•showPreload,默认值:true,是否显示加载提示Loading zoom(选项: ‘true','false')。

•preloadText,默认值:'Loading zoom',自定义加载提示文本。

•preloadPosition,默认值:'center',加载提示的位置,值也可以为'bycss',以通过css指定位置。

样式的个性化当然也可以直接修改jqzoom.css文件。

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

前端热敏纸小票打印遇到乱码问题怎么办? 前端热敏纸小票打印遇到乱码问题怎么办? Apr 04, 2025 pm 02:42 PM

前端热敏纸小票打印的常见问题与解决方案在前端开发中,小票打印是一个常见的需求。然而,很多开发者在实...

神秘的JavaScript:它的作用以及为什么重要 神秘的JavaScript:它的作用以及为什么重要 Apr 09, 2025 am 12:07 AM

JavaScript是现代Web开发的基石,它的主要功能包括事件驱动编程、动态内容生成和异步编程。1)事件驱动编程允许网页根据用户操作动态变化。2)动态内容生成使得页面内容可以根据条件调整。3)异步编程确保用户界面不被阻塞。JavaScript广泛应用于网页交互、单页面应用和服务器端开发,极大地提升了用户体验和跨平台开发的灵活性。

谁得到更多的Python或JavaScript? 谁得到更多的Python或JavaScript? Apr 04, 2025 am 12:09 AM

Python和JavaScript开发者的薪资没有绝对的高低,具体取决于技能和行业需求。1.Python在数据科学和机器学习领域可能薪资更高。2.JavaScript在前端和全栈开发中需求大,薪资也可观。3.影响因素包括经验、地理位置、公司规模和特定技能。

如何使用JavaScript将具有相同ID的数组元素合并到一个对象中? 如何使用JavaScript将具有相同ID的数组元素合并到一个对象中? Apr 04, 2025 pm 05:09 PM

如何在JavaScript中将具有相同ID的数组元素合并到一个对象中?在处理数据时,我们常常会遇到需要将具有相同ID�...

JavaScript难以学习吗? JavaScript难以学习吗? Apr 03, 2025 am 12:20 AM

学习JavaScript不难,但有挑战。1)理解基础概念如变量、数据类型、函数等。2)掌握异步编程,通过事件循环实现。3)使用DOM操作和Promise处理异步请求。4)避免常见错误,使用调试技巧。5)优化性能,遵循最佳实践。

如何实现视差滚动和元素动画效果,像资生堂官网那样?
或者:
怎样才能像资生堂官网一样,实现页面滚动伴随的动画效果? 如何实现视差滚动和元素动画效果,像资生堂官网那样? 或者: 怎样才能像资生堂官网一样,实现页面滚动伴随的动画效果? Apr 04, 2025 pm 05:36 PM

实现视差滚动和元素动画效果的探讨本文将探讨如何实现类似资生堂官网(https://www.shiseido.co.jp/sb/wonderland/)中�...

console.log输出结果差异:两次调用为何不同? console.log输出结果差异:两次调用为何不同? Apr 04, 2025 pm 05:12 PM

深入探讨console.log输出差异的根源本文将分析一段代码中console.log函数输出结果的差异,并解释其背后的原因。�...

前端开发中如何实现类似 VSCode 的面板拖拽调整功能? 前端开发中如何实现类似 VSCode 的面板拖拽调整功能? Apr 04, 2025 pm 02:06 PM

探索前端中类似VSCode的面板拖拽调整功能的实现在前端开发中,如何实现类似于VSCode...

See all articles