Expanding Multi-State Boxes - Horizontally?

Hello All

I am looking to create an expanding multi state element. It would essentially be a box that expands out to be double the width once clicked and return after closing back to the square base state. However as of current I am unable to create this using multi-state elements. It seems that only vertical expansion is possible for multi-state elements?

Are there any other ways to have an element like this that at base state is a square, then when clicking a button expands out into double the width horizontally?

Thanks,
Megan Dale

Hi Megan, you’re right that a multi-state box only grows downwards with its content, it won’t stretch sideways between states.

Two ways that work:

  1. Two elements instead of one. Put the square box and a double-width box (same top/left position) on the page, hide the wide one on load, and swap them with Velo:
$w.onReady(() => {
  $w('#wideBox').hide();
  $w('#openBtn').onClick(() => {
    $w('#squareBox').hide('fade', { duration: 150 });
    $w('#wideBox').show('slide', { direction: 'left', duration: 300 });
  });
  $w('#closeBtn').onClick(() => {
    $w('#wideBox').hide('slide', { direction: 'left', duration: 300 });
    $w('#squareBox').show('fade', { duration: 150 });
  });
});

The slide effect makes it look like it unfolds sideways. Make sure nothing sits to the right of it, otherwise it will overlap.

  1. In Studio, CSS. If you’re on Wix Studio you can give the container a class in the global.css and animate width on a class change ($w('#box').customClassList.add('wide') / .remove('wide')), with transition: width .3s ease. That gives a real smooth width change, which the built-in effects can’t do.

Option 1 is simpler and works in the classic Editor too.

Amazing, thank you Caner!

I will definitely look into doing some work with the custom CSS animation classes as I have a bit of a rough transition between multi-box states that seem to flash when the elements in one box are the same in the second state (with content added of course).

Best,
Megan Dale

Glad it helped! On the flash: a multi-state box swaps whole states, so even elements that look identical in both states are separate copies, and the old ones disappear before the new ones paint. Two things that usually smooth it out:

  • Pull the shared elements out of the box. Put the parts that stay the same (title, image, background) on top of the multi-state box instead of inside each state, and keep only the content that changes in the states. Nothing to redraw, nothing to flash.
  • Fade the change yourself. Instead of a plain changeState, hide the box, change state, then show it:
async function goTo(stateId) {
  const box = $w('#myStateBox');
  await box.hide('fade', { duration: 120 });
  box.changeState(stateId);
  await box.show('fade', { duration: 180 });
}

A short fade reads as a deliberate transition rather than a glitch. Also worth checking: images in the second state loading late can look like a flash too, so smaller image files help.