二、新建 /static/js/xgt-xiangqing.js文件,代码如下:
- "use strict";
- $(function(){
- var content_Height = $(window).height()-41;
- $(".content").css({
- "height":content_Height
- });
-
- var selfAdaption = function(){
- /* 自适应 */
- var mHeight = $(window).height()-230;
- var iWidth = $(window).width()-$(".imgbox-right").width()-90;
- var mg = $(window).width()*0.4;
- var imgheight =parseInt($(".imgbox-left-box").find("img").attr("height"));//图片的高
- var imgwidth = parseInt($(".imgbox-left-box").find("img").attr("width"));//图片的宽
- var imgBL = imgwidth/imgheight;
- if(iWidth<=416){
- iWidth=416;
- };
- if(mHeight<416){
- mHeight = 416+110
- };
- $(".imgbox-left").css({
- "width":iWidth,
- "height":mHeight
- });
- if($(window).width()<=1370){
- mg=mg-200
- }else{
- mg = mg+100
- }
- if(imgheight>=mHeight-110){//图片高度大于盒子高-110 且 盒子高*0.8/imgBL大于盒子高-110
-
- if(iWidth*0.8/imgBL >= mHeight-110){
- $(".imgbox-left-box").find("img").css({
- "width":(mHeight-110)*imgBL,
- "height":mHeight-110,
- });
-
- $(".imgbox-left-box").css({
- "width":(mHeight-110)*imgBL,
- "height":mHeight-110,
- "top":"50%",
- "left":"50%",
- "margin-left":-(mHeight-110)*imgBL/2,
- "margin-top":-(mHeight-110)/2-30
- });
- // console.log("图片高度大于盒子高-110 且 盒子高*0.8/imgBL大于盒子高-110")
- }else{
- $(".imgbox-left-box").find("img").css({
- "width":iWidth*0.8,
- "height":iWidth*0.8/imgBL,
- });
-
- $(".imgbox-left-box").css({
- "width":iWidth*0.8,
-
- "height":iWidth*0.8/imgBL,
- "top":"50%",
- "left":"50%",
- "margin-left":-iWidth*0.8/2,
- "margin-top":-iWidth*0.8/imgBL/2-30
- });
- //console.log("图片-110")
- };
-
- }else if( iWidth*0.8/imgBL < mHeight-110 ){
- $(".imgbox-left-box").find("img").css({
- "width":iWidth*0.8,
- "height":iWidth*0.8/imgBL,
- });
-
- $(".imgbox-left-box").css({
- "width":iWidth*0.8,
- "height":iWidth*0.8/imgBL,
- "top":"50%",
- "left":"50%",
- "margin-left":-iWidth*0.8/2,
- "margin-top":-iWidth*0.8/imgBL/2-30
- });
- //console.log("图片-220")
- }else{
- $(".imgbox-left-box").find("img").css({
- "width":imgwidth,
- "height":imgheight,
- });
-
- $(".imgbox-left-box").css({
- "width":imgwidth,
- "height":imgheight,
- "top":"50%",
- "left":"50%",
- "margin-left":-imgwidth/2,
- "margin-top":-imgheight/2-30
- });
- //console.log("图片-330")
- };
- $(".imgbox-right").height(mHeight+109);
- if($(window).width()<=1200){ //悬浮表单按钮
- $(".img-from").addClass("img-fromSM");
- $(".img-from").find(".img-from-price").css({
- "margin-left":25
- });
- $(".img-from").find(".img-from-biao").css({
- "margin-top":10
- });
- }else{
- $(".img-from").removeClass("img-fromSM");
- $(".img-from").find(".img-from-biao").css({
- "margin-top":0
- });
- $(".img-from").find(".img-from-price").css({
- "margin-left":0
- });
- };
- $(window).resize(function(){
- //盒子自适应
- var mLeft = $(window).width()-$(".imgbox-right").width()-90;//图片盒子的宽
- var mHeight = $(window).height()-230; //图片盒子的高
- var mg = $(window).width()*0.4; //分享 微信 放大图片的 定位
- var ml = 0;
- if(mLeft<=416){
- mLeft = 416;
- ml = 0;
- }else{
- ml = 30;
- }
- if(mHeight<=416){
- mHeight = 416;
- };
- $(".imgbox-left").css({
- "width":mLeft,
- "height":mHeight,
- "margin-left":ml
- });
-
- if($(window).width()<=1370){
- mg=mg-200
- }else{
- mg = mg+100
- }
-
-
- //分享 微信 查看原图
- if($(window).width()<=980){
- $(".path").css({
- "height":"auto"
- });
- }else{
- $(".path").css({
- "height":"40px"
- });
- };
- $(".imgbox-right").height(mHeight+109);
- if($(window).width()<=1200){
- $(".img-from").addClass("img-fromSM");
- $(".img-from").find("p").css({
- "margin-top":10
- });
- }else{
- $(".img-from").removeClass("img-fromSM");
- $(".img-from").find("p").css({
- "margin-top":0
- });
- };
-
- var bww = $(".imgbox-left").width()+ml+34+258;
- $("#ascrail2000").css({
- "left":bww
- });
-
- });
- };
- //selfAdaption()//初始化
-
- var displayImg = {
- init:function(){
- this.$parents = $('.imgbox'); //幻灯片外层容器
- this.$imgBox = $(".imgbox-left-box");//容器盒子
- this.$bigImg = $(".img-big");//大图
- this.$imgSc = $(".img-sc");//收藏
- this.$bigPrevBtn = $('.imgbox-left-prev'); //大图向左按钮
- this.$bigNextBtn = $('.imgbox-left-next'); //大图向右按钮
- this.$smallImgBox = $('.imgList'); //所有小图容器,包含左右按钮
- this.$scrollSmallBox = $('.imgList-box ul'); //小图滚动容器
- this.$smallPrevBtn = $('.imgList-left'); //小图向左按钮
- this.$smallNextBtn = $('.imgList-right'); //小图向右按钮
- this.$smallImgLi = $('.imgList-box li'); //小图列表li
- this.$smallImg = $('.imgList-li img'); //所有小图
- this.listWidth = 90*this.$smallImgLi.length; //滚动容器宽度
- this.smallImgIndex = 0; //默认展示第一张小图
-
- this.$scrollSmallBox.width(this.listWidth).css({'margin-left':0});
- this.$smallImgLight = this.$smallImgLi.eq(this.smallImgIndex).addClass('active');
- this.createBigImg(this.smallImgIndex); //显示大图
- this.imgClick();
- this.btnDisplay();
- this.tabLabel(this,0);
- },
- /**
- * 鼠标移入显示分类,鼠标移出隐藏分类
- */
- displayClassify : function(){
- var timer = null,
- $classify_icon = $('.path .classify_icon'),
- $classify = $('.path .classify');
- $('.path .classify_icon,.path .classify').hover(function(){
- clearTimeout(timer);
- $classify_icon.addClass('active');
- $classify.show();
- },function(){
- $classify_icon.removeClass('active');
- timer = setTimeout(function(){
- $classify.hide();
- },100)
- });
- },
- BigImgUrl : function(index){ //获取小图data-src里的链接
- return this.$smallImgLi.eq(index).find(".lazy").attr("data-src");
- },
- /**
- * 计算大图的大小显示
- */
- bigImgSize: function (imgW, imgH, divW, divH) {
- var scale = imgW / imgH,
- scale2 = divW / divH,
- w,
- h;
- scale > scale2 ? ( w = divW, h = divW / scale ) : ( w = scale * divH, h = divH );
- return {
- w: w > imgW ? imgW : w,
- h: h > imgH ? imgH : h
- }
- },
- /**
- * 创建大图
- * @param index 创建大图需要的索引
- */
- createBigImg:function(index){
- var _this = this, //保存this
- $smallImg = this.$smallImg.eq(index),
- imgSrc = $smallImg.attr('data-src'),
- bigImgAlt = $smallImg.attr('data-title'),
- bigImgGid = $smallImg.attr('data-gid'),
- imgHtml = '<img data-gid="'+bigImgGid+'" title="'+bigImgAlt+'" src="'+imgSrc+'" id="bigImg" alt="'+bigImgAlt+'">',
- weixinQrSrc = 'http://weixin.17house.com/index.php?g=Home&m=Api&a=creatQr&restype=img&content=恭喜您收藏了'+bigImgAlt+',%3Ca+href%3D%27http%3A%2F%2Fm.17house.com%2Fxiaoguotu%2F'+bigImgGid+'.html%27%3E点击查看%3C%2Fa%3E';
- // 微信二维码加载
- $('.weixin-img').attr('src', weixinQrSrc);
-
- // 大图加载完获取实际宽高
- var newImg = new Image();
- newImg.setAttribute("id","bigImg");
- newImg.onload = function(){
- var imgWidth = this.width, //图片实际宽度
- imgHeight = this.height; //图片实际高度
- var bigImgSize = _this.bigImgSize(imgWidth, imgHeight, 825, 510); //计算大图比例
- _this.$bigImg.remove();
- _this.$imgBox.prepend(imgHtml);
- _this.$bigImg = $('#bigImg');
- //设置大图宽高
- _this.$bigImg.css({
- "width": bigImgSize.w,
- "height":bigImgSize.h,
- "opacity":1
- }).attr({
- 'src' : imgSrc,
- 'title' : bigImgAlt,
- 'data-gid' : bigImgGid,
- "height":imgHeight,
- "width":imgWidth
- });
- //判断次图片是否收藏
- if($(".siteNavBgRS").css("display")=="block"){
- var uid = CommonCookie.getCookie("user");
- var imgid = bigImgGid;
- var urlP = "http://www.17house.com/xiaoguotu/iscollect/"+uid+"/"+imgid+"/";
- $.getJSON(urlP, function(data) {
- if(data.status == 1){
- $(".img-sc").hide();
- $(".img-sch").show();
- }else{
- $(".img-sc").show();
- $(".img-sch").hide();
- }
- });
- }else{
- $(".img-sc").show();
- $(".img-sch").hide();
- };
- //设置大图容器宽高
- _this.$imgBox.css({
- "width": bigImgSize.w,
- "height":bigImgSize.h
- });
- selfAdaption() //大图替换后格式化
- //查看大图
- $('.big_img a').attr({'href':imgSrc});
- }
-
- newImg.src = imgSrc;
-
- // 页面title添加
- if( bigImgAlt ) {
- document.title = bigImgAlt+'-一起装修网装修效果图';
- }
- // 改变页面url
- var thisurl = "http://"+window.location.host+"/case/detail-" + bigImgGid + ".html";
- if (window.history.pushState) {
- window.history.pushState({ pageIndex: 1 }, bigImgAlt);//, thisurl
- return true;
- }
- },
- imgClick : function(){
- var _this = this;
- //点击图片切换
- this.$smallImg.each(function(index,ele){
- var $target = $(ele);
- $target.bind('click',function(){
- _this.$smallImgLi.removeClass('active').eq(index).addClass('active');
- _this.createBigImg(index);
- _this.smallImgIndex = index;
- _this.tabLabel(_this,_this.smallImgIndex);
- });
- });
-
- //点击左右按钮切换
- this.$parents.delegate('.btn','click',function(ev){
- var $target = $(this),
- marginLeft = parseInt(_this.$scrollSmallBox.css('margin-left'));
- if($target.hasClass('btn_next')){//向右
- _this.smallImgIndex++;
- if(_this.smallImgIndex == _this.$smallImgLi.length){
- var urlA = _this.$scrollSmallBox.find("li.imgList-li:last").attr("last");
- window.location.href=urlA;
- };
- if(_this.smallImgIndex > 3){
- marginLeft = (_this.smallImgIndex - 3) * 90;
- if(!_this.$scrollSmallBox.is(':animated')){
- _this.$scrollSmallBox.stop(false,true).animate({'marginLeft' : -marginLeft});
- };
- };
-
- }else{//向左
- _this.smallImgIndex--;
- if(_this.smallImgIndex < 0){
- _this.smallImgIndex = 0;
- }
- if(_this.smallImgIndex >= 3){
- marginLeft += 90;
- if(!_this.$scrollSmallBox.is(':animated')){
- if(parseInt(_this.$scrollSmallBox.css("margin-left"))>=0){
- marginLeft=0
- }
- _this.$scrollSmallBox.stop(false,true).animate({'marginLeft' : marginLeft});
- }
- };
- if(_this.smallImgIndex < 3 && parseInt(_this.$scrollSmallBox.css("margin-left"))<0){
- marginLeft = 0;
- if(!_this.$scrollSmallBox.is(':animated')){
- _this.$scrollSmallBox.stop(false,true).animate({'marginLeft' : marginLeft});
- }
- };
-
- }
-
- _this.$smallImgLi.removeClass('active').eq(_this.smallImgIndex).addClass('active');
- _this.createBigImg(_this.smallImgIndex);
- _this.tabLabel(_this,_this.smallImgIndex);
- ev.stopPropagation();
- });
- },
- btnDisplay : function(){
- var _this = this;
- this.$parents.hover(function(){
- _this.$bigPrevBtn.show();
- _this.$bigNextBtn.show();
- $('.tp_design,.tp_collect').show();
- },function(){
- _this.$bigPrevBtn.hide();
- _this.$bigNextBtn.hide();
- $('.tp_design,.tp_collect').hide();
- })
- },
- //切换图片 标签价格 跟着变
-
- }
- displayImg.init()//初始化
- displayImg.displayClassify();
-
- tuPopTk.sheji();//设计报名
-
-
- })
复制代码
|