【问题标题】:How to store a JavaScript variable inside of an object as a property如何将 JavaScript 变量作为属性存储在对象内部
【发布时间】:2018-09-10 20:20:15
【问题描述】:

我正在尝试动态获取 DOM 中图像的宽度,然后在先前声明的 JavaScript 对象中存储/覆盖现有属性。我能够获得页面上图像的宽度,并且它似乎已正确存储在我的对象 defControls 中,但我的 #showcase 的宽度仍为 600px。对此的任何帮助将不胜感激!

// USER EDITABLE CONTROLS
    var defControls = {
        content : 'img',
        showControls : true,
        prevText : '« Previous', // previous button text
        nextText : 'Next »', // next button text
        containerWidth : 600 // property to be determined by image in the DOM
    };

    // VARIABLE DECLARATIONS
    var controls = {};

    // CHECKS FOR userControls
    if (typeof userControls !== 'undefined') {
        var controls = Object.assign({}, defControls, userControls);
    } else {
        controls = defControls;
    }
    
    var contentType = $(controls.content);
    var $el = $('#showcase');
    var $leftArrow = '#left_arrow';
    var $rightArrow = '#right_arrow';
    var $load = $el.find(contentType)[0];
    
    // PRELOADS CAROUSEL WITH CORRECT SETTINGS
    $el.css('width', controls.containerWidth);
    $load.className = 'active';

    $(window).on('load', function () {
        if (controls.content == 'img') {
            controls.containerWidth = $el.children().width();
            console.log($el.children().width()); // fetches the width of the DOM img
            console.log(controls.containerWidth); // shows the width is being updated but the #showcase div remains at 600px
        }
    })
    
    // ADD CAROUSEL CONTROLS TO PAGE
    if (controls.showControls === true) {
        $('<div id="controls"><a href="#" id="' + $leftArrow.replace('#', '') + '">' + controls.prevText + '</a> <a href="#" id="' + $rightArrow.replace('#', '') + '">' + controls.nextText + '</a></div>').insertAfter('#showcase');
        $('#controls').find('#left_arrow').addClass('disabled');
    }
* {
	margin: 0px;
	padding: 0px;
}

#showcase {
	overflow: hidden;
	background: green;
}

img {
	width: 268px; /* Temporary - image width will be adjusted in the JS */
}

a {
	color: blue;
}

.disabled {
	color: red !important;
}

.slide {
	display: none;
	opacity: 0;
	position: relative;
	left: 0px;
	right: 0px;
}

.active {
	display: inline-block;
	opacity: 1;
	position: relative;
	left: 0px;
	right: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="showcase">
	<img class="slide" src="https://picsum.photos/458/354/?image=306" />
	<img class="slide" src="https://picsum.photos/458/354/?image=626" />
	<img class="slide" src="https://picsum.photos/458/354/?image=806" />
</div>

【问题讨论】:

  • 但是你没有做任何事情来改变#showcase的宽度那么你为什么期望宽度改变?
  • @Stakvino,这就是controls.containerWidth = $el.children().width(); 应该做的。它在 DOM 中获取 img 的宽度并将其分配给controls.containerWidth

标签: javascript html object variables properties


【解决方案1】:

但我的#showcase 的宽度仍为 600 像素

是的,您没有再次更新它。没有办法让一个属性(或变量)成为一个神奇的参考,这样当你更新它时,展示的宽度也会发生变化。

获取宽度后直接修改css即可:

$(window).on('load', function () {
    if (controls.content == 'img') {
        controls.containerWidth = $el.children().width(); // fetches the width of the DOM img
        console.log(controls.containerWidth); // shows the updated width
        $el.css('width', controls.containerWidth); // updates the #showcase div with the new value
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    }
})

【讨论】:

  • 我应该想到这样做。我太专注于让 containerWidth 属性在 Object 中更新。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多